Java Reference
In-Depth Information
8. }
9.
10. public static void main(String [] args) {
11. ReturnDemo demo = new ReturnDemo();
12. File file = demo.getFile(args[0]);
13.
14. if(file.exists()) {
15. System.out.println(file.getName() + “ file exists”);
16. } else {
17. System.out.println(file.getName() + “ doesn't exist”);
18. }
19.
20. file = null;
21. }
22. }
The getFile method returns the reference f , which points to a new File object. Keep
in mind that this File object is on the heap, not in the method's call stack memory, so the
File object is not destroyed when getFile returns. The local variable file in main gets a
copy of f when getFile returns. The File object from line 6 does not become eligible for
garbage collection until after line 20.
The ReturnDemo program demonstrates a method that instantiates an object and returns
a reference to that object. This is a common occurrence in Java. Just remember that the
object is on the heap (all objects are instantiated on the heap!) and a copy of the reference
is returned to the calling method. As with method arguments, the largest piece of data that
can be returned from any Java method is 64 bits (a long or double ). The fact that Java only
allows call by value is an attempt to simplify the language. There is never any confusion
with arguments and parameters: the parameter is always a copy of the argument.
Now that we have discussed the details of call by value, we turn our attention to another
objective in the “Fundamentals” section: the Java operators.
Java Operators
You need to be able to “write code that correctly applies the appropriate operators.” This
section discusses the Java operators that appear on the exam. Table 1.2 lists all of the 41
operators in Java 6.0, listed in their order of precedence . Order of operations in Java is well
defi ned, and the operators are guaranteed to be evaluated in the order shown. If operators
have the same level of precedence, Java guarantees evaluation in left-to-right order.
Search WWH ::




Custom Search