Java2 Certification
|
|
You can discuss this topic with others at http://www.jchq.net/discus
Read reviews and buy a Java Certification book at http://www.jchq.net/bookreviews/jcertbooks.htm
Determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.
This objective appears to be asking you to understand what happens when you
pass a value into a method. If the code in the method changes the variable, is
that change visible from outside the method?. Here is a direct quote from Peter
van der Lindens Java Programmers FAQ (available at http://www.afu.com)
//Quote
All parameters (values of primitive types and values that are references to
objects) are passed by value. However this does not tell the whole story, since
objects are always manipulated through reference variables in Java. Thus one
can equally say that objects are passed by reference (and the reference
variable is passed by value). This is a consequence of the fact that variables
do not take on the values of "objects" but values of "references
to objects" as described in the previous question on linked lists.
Bottom line: The caller's copy of primitive type arguments (int, char, etc.)
_do not_ change when the corresponding parameter is changed. However, the
fields of the caller's object _do_ change when the called method changes the
corresponding fields of the object (reference) passed as a parameter.
//End Quote
If you are from a C++ background you will be familiar with the concept of
passing parameters either by value or by reference using the & operator.
There is no such option in Java as everything is passed by value. However it
does not always appear like this. If you pass an object it is an object
reference and you cannot directly manipulate an object reference.
Thus if you manipulate a field of an object that is passed to a method it has
the effect as if you had passed by reference (any changes will be still be in
effect on return to the calling method)..
Take the following example
class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ ValHold v = new ValHold(); v.i=10; System.out.println("Before another = "+ v.i); another(v); System.out.println("After another = "+ v.i); }//End of amethod public void another(ValHold v){ v.i = 20; System.out.println("In another = "+ v.i); }//End of another }
The output from this program is
Before another = 10 In another = 20 After another = 20
See how the value of the variable i has been modified. If Java always passes by value (i.e. a copy of a variable), how come it has been modified? Well the method received a copy of the handle or object reference but that reference acts like a pointer to the real value. Modifications to the fields will be reflected in what is pointed to. This is somewhat like how it would be if you had automatic dereferencing of pointers in C/C++.
When you pass primitives to methods it is a straitforward pass by value. A method gets its own copy to play with and any modifications are not reflected outside the method. Take the following example.
public class Parm{ public static void main(String argv[]){ Parm p = new Parm(); p.amethod(); }//End of main public void amethod(){ int i=10; System.out.println("Before another i= " +i); another(i); System.out.println("After another i= " + i); }//End of amethod public void another(int i){ i+=10; System.out.println("In another i= " + i); }//End of another }
The output from this program is as follows
Before another i= 10 In another i= 20 After another i= 10
|
|
Given the following code what will be the output?
class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ int i = 99; ValHold v = new ValHold(); v.i=30; another(v,i); System.out.println(v.i); }//End of amethod public void another(ValHold v, int i){ i=0; v.i = 20; ValHold vh = new ValHold(); v = vh; System.out.println(v.i+ " "+i); }//End of another }
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
4) 10,0,20
This topic is covered in the Sun Tutorial at http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html Jyothi Krishnan on this topic at http://www.geocities.com/SiliconValley/Network/3693/obj_sec5.html#obj18 |
Last updated
11 Jan 2000
copyright © Marcus Green 2000