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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Q: Is it possible to use switch with range of values?<br />

Ex:<br />

switch (iBegins){<br />

case 2 to 10:<br />

}<br />

Answer:<br />

not exactly like this but:<br />

switch (iBegins){<br />

case 2:<br />

case 3:<br />

case 4:<br />

case 5:<br />

case 6:<br />

case 7:<br />

case 8:<br />

case 9:<br />

case 10:<br />

/* do stuff */<br />

break;<br />

case 11:<br />

/* other stuff */<br />

}<br />

Jan Schaumann <br />

Q: Is there a general reason why nullpointer exceptions occur?<br />

Answer: Of course there is. A NullPointerException happens when you have a<br />

reference, it's set to null (or is a class or instance variable and has never been<br />

initialized), and you try to perform some operation on the object it points to. Since it<br />

really doesn't point to an object, you can't do that.<br />

--<br />

Chris Smith<br />

Q: I am aware that the exceptions in the catch statements must be of the same<br />

type or a super type of the exception thrown in the try block.<br />

My question is this:<br />

Is there any significance in the ordering of the catch statements after a try block?<br />

Answer: It is required to design you catch statement sequences with most derived<br />

exception class type first and progressing to the highest superclass. Else, the code<br />

will not compile.<br />

For a obvious reason, if the a catch block for a given class precedes a catch block for<br />

a type that is derived from the first, the second catch block will never be executed.<br />

--<br />

D. Michael Nelson<br />

Q: I wrote a program that use a few RS232 ports. The operators are unskilled<br />

and often start multiple instances of the program. Will someone please be so kind<br />

and tell me how I can prevent them doing it?<br />

Answer 1: The first instance might write a file. Subsequent instances could check for<br />

the existence of that file, or else check it's contents.<br />

Another method could involve creating a server socket on a specific port. Subsequent<br />

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

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

Saved successfully!

Ooh no, something went wrong!