27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

General <strong>Java</strong> Questions I<br />

System.err no. If you want to put both the "normal" output and the "error" output to a<br />

file you must use the special redirect 2>.<br />

This allow you to send normal messages into a file or in the /null black hole, but still<br />

receive the error messages on the console.<br />

What is the essential difference between an abstract class and an interface?<br />

What dictates the choice of one over the other?<br />

Answer: You can only extend one class (abstract or not) whereas you can always<br />

implement one or more interfaces. Interfaces are <strong>Java</strong>'s way to support multiple<br />

inheritance.<br />

Does anyone know how could I get the size of an Enumeration object? The API<br />

for Enumeration only contains getNext() and next().<br />

Answer 1: You can't. Theoretically, some classes that implement Enumeration may<br />

also provide some way to get a size, but you'd have to know about the more specific<br />

run-time type and cast to it... and none of the standard java.util Collections classes<br />

nor Vector or such provide these methods in their Enumeration implementations.<br />

Answer2:<br />

you can make your own class like this:<br />

import java.util.*;<br />

public class MyEnumeration{<br />

int size;<br />

int index = 0;<br />

Enumeration e;<br />

}<br />

public MyEnumeration(Vector v){<br />

size = v.size();<br />

e = v.elements();<br />

index = 0;<br />

}<br />

public boolean hasMoreElements(){<br />

return e.hasMoreElements();<br />

}<br />

public Object nextElement(){<br />

index++;<br />

return e.nextElement();<br />

}<br />

public int size(){<br />

return size;<br />

}<br />

public int getIndex(){<br />

return index;<br />

}<br />

file:///F|/350_t/350_tips/general_java-I.htm (8 of 31) [2002-02-27 21:18:17]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!