![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Passing arguments to methods There are two ways to pass an argument to a method: by value and by reference. The difference is in what happens to the variable passed in the calling method as a result of whats done to it in the called method. For example, consider this code fragment: int a = 7; changeVariable(a); System.out.println(a); Now suppose the changeVariable() method looks like this: public void changeVariable(int a) { a = 10; } What value gets printed 7 or 10? If the argument a is passed by value, then a copy of variable as value is used by the changeVariable() method. The changeVariable() method never gets access to the original variable a in the calling method. It has a different variable, also named a. Therefore, the calling method prints the value 7. On the other hand, if a is passed by reference, then the changeVariable() method does not get a copy of the variable named a. It gets the real thing. The name a in the calling method and the name a in the changeVariable() method refer to the same variable. Therefore, System.out.println() prints 10. Note that the names arent important here. If changeVariable() were written using i or some name other than a for its argument, the result would be the same. What makes the difference is whether the variable is passed by reference or by value. If a variable is passed by reference, it can change in the calling method. If its passed by value, it cannot. Java passes all arguments by value, not by reference. Because object and array variables in Java are references to the object or array, it can appear as if an object is passed by reference if you modify only the fields of the object or array, but do not change the reference itself. For example, consider this program: import java.awt.Point; class changePoint { public static void main(String args[]) { Point p1 = new Point(0, 0); changePoint(p1); System.out.println(p1); } static void changePoint(Point p) { p.x = 38; p.y = 97; } } This program prints java.awt.Point[x=38,y=97] The point has therefore been changed. However, the reference, which is what was really passed, has not been changed. To see that, consider the following program: import java.awt.Point; class dontChangePoint { public static void main(String args[]) { Point p1 = new Point(0, 0); dontChangePoint(p1); System.out.println(p1); } static void dontChangePoint(Point p) { p = new Point(38, 97); } } It prints the following: java.awt.Point[x=0,y=0]
In this example, a copy of the reference p1 was passed to the dontChangePoint() method. A new Point object was then assigned to that copy. This, however, did not change the old reference in the main() method. In the previous example, the reference p in the changePoint() method and p1 in the main() method both referred to the same object. In this example, p and p1 refer to different objects after the new Point is assigned to p. Special references There are three special references in Java source code: null, this, and super. The meaning of these references generally depends on their context. The null reference The null reference is an invalid reference. It has no type, and thus may be assigned to a variable of any reference type. When a reference variable is declared but not constructed, it is initially equal to null.
|
![]() |
|