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 II<br />

ordered path? Vector does this but i need the key/value functionality. hashmaps do<br />

not guarantee the order of the objects.<br />

Answer: Take a look at java.util.TreeMap.<br />

Red-Black tree based implementation of the SortedMap interface. This class<br />

guarantees that the map will be in ascending key order, sorted according to the<br />

natural order for the key's class (see Comparable), or by the comparator provided at<br />

creation time, depending on which constructor is used.<br />

Note that this implementation is not synchronized. If multiple threads access a map<br />

concurrently, and at least one of the threads modifies the map structurally, it must be<br />

synchronized externally.<br />

Most people asked why there is an error, but my question is why this is NOT an<br />

error<br />

Please take a look:<br />

r is a number and s is a character, why can I put them together to make a comparison<br />

without compilation error? Could somebody tell me... thank you<br />

double r = 34.5;<br />

char s = 'c';<br />

if (r > s) {<br />

System.out.println("r > s");<br />

} else {<br />

System.out.println("r < s");<br />

}<br />

Answer: Yes, char is indeed a 16-bit value. However, the actual answer is in the <strong>Java</strong><br />

Language Specification, section 5.6.2, which is at the following URL:<br />

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#170983<br />

In summary, the char is automagically promoted to a double. No explicit cast is<br />

necessary since the language rules say that it gets "promoted" to a double<br />

by John O'Conner<br />

Q: == and equals ()... These two still make me confuse a lot of time.<br />

Can somebody give me some thumb rule or explain it to me?<br />

Answer: When you use == with a primitive -int, double, char, ... you are checking that<br />

the values are identical. But if you use == with an object, you are checking that the 2<br />

objects are stored at the same address. In other words the references pointing to the<br />

same object...<br />

Method equals () is different.<br />

It is the same as ==, if it isn't overriden by the object class.<br />

Many classes override the method equals (). In this case this method will check that<br />

content of the object is the same or not, not addresses.<br />

Q: Why do I get message like “wrong magic number” when I am trying to run<br />

applet? What is magic number?<br />

Answer: The first thing a JVM does when it loads a class is check that the first four<br />

bytes are (in hex) CA FE BA BE. This is the "magic number" and thats why you are<br />

file:///F|/350_t/350_tips/general_java-II.htm (7 of 13) [2002-02-27 21:18:24]

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

Saved successfully!

Ooh no, something went wrong!