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

} else {<br />

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

}<br />

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

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

getting that error, you are trying to load a file that isnt a class and so the class loader<br />

in the JVM is throwing out that exception.<br />

Make sure you transfer the class files to site in binary mode, rather than text or ASCII<br />

mode.<br />

An error from the browser saying "cannot start applet ... bad magic number" usually<br />

means that one of the class files on the server is corrupted. '<br />

Replace your class binary files on the web server; clean up the cache of your<br />

browser, and reload your applet.<br />

Q: In java, I found a lot of methods, which let you enter a line (read (), readLine ()<br />

e.c.t). They all wait until return is pressed, and then start providing you the<br />

information.<br />

Does anyone know if there is a read method available whit the desired behaviour, i.e.<br />

which doesn't wait for return being pressed?<br />

Answer: <strong>Java</strong> does not provide it, the terminal itself waits until return is pressed<br />

before sending the entered line to <strong>Java</strong>.<br />

You need to use some platform specific mechanism to change the terminal settings.<br />

Q: Can I pass a function as a parameter in <strong>Java</strong>? If so, how?<br />

Answer: No, you cannot. But you can pass an object with method and then just use<br />

file:///F|/350_t/350_tips/general_java-I.htm (10 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!