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

Note that a constructor - or any method in general - throwing an exception will not<br />

"return null", but will leave the "assign target" as it was.<br />

Tor Iver Wilhelmsen<br />

Q: What does mean "volatile"?<br />

For the past couple of hours, I've seen quite a few set of codes that has the _volatile_<br />

keyword.<br />

E.g.<br />

private volatile somethingsomething....<br />

What does it mean?<br />

Answer: See JLS 2nd Edition, which just came out last year, still mentions it.<br />

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

"A field may be declared volatile, in which case a thread must reconcile its working<br />

copy of the field with the master copy every time it accesses the variable. Moreover,<br />

operations on the master copies of one or more volatile variables on behalf of a<br />

thread are performed by the main memory in exactly the order that the thread<br />

requested."<br />

Seems just like the idea in C++ and appears still to me present in the language.<br />

Synchronization certainly has it place in many applications, that doesn't mean that<br />

volatile is not longer used or part of the language.<br />

--<br />

Paul Hill<br />

Q: If some method is deprecated does it mean that one get a chance not to find<br />

this method in some <strong>Java</strong> version?<br />

Answer: It means it *may* be dropped in a future version, but chances are it's still<br />

there. I'm not sure I've seen any deprecated features actually being removed from an<br />

API yet - often they're turned into no-ops first, if they're dangerous.<br />

Jon Skeet - http://www.pobox.com/~skeet<br />

this advice first was published on comp.lang.java.programmer<br />

Q: suppose I put a file a.txt in package com.xyz and the try access it like following.<br />

Will it work?<br />

import com.xyz.*;<br />

public class Hello{<br />

File f = new File("a.txt");<br />

...<br />

}<br />

it is not working for me. Is there any workaround?<br />

Answer: If the source and the text file are in the jar file, then you access the file by:<br />

URL fileURL = getClass().getResource("file.txt");<br />

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