9.3 PASSING ARGUMENTS IN JAVA


9.3 PASSING ARGUMENTS IN JAVA

As was mentioned at the beginning of this chapter, Java gives you only one mode for passing arguments to methods—pass by value. For class type arguments, a more precise way to talk about this argument passing mode is to describe it as pass by value of object reference. To explain what that means, let's consider separately the two cases: passing primitive type arguments and passing class type arguments.

9.3.1 Passing a Primitive Type Argument by Value

The following example shows that there is no difference between how a primitive argument is passed by value in C++ and how it is passed (by value) in Java.

 
//PassPrimByValue.java class Test { public static void main(String[] args) { int x = 100; //(A) g(x); //(B) System.out.println(x); // outputs 100 } static void g(int y) { y++; } //(C) }

As was true for the same case in C++, the change made to the local variable in line (C) in the body of the function g(int) is not visible in main. Therefore, the print statement in main will print out the value of x as set originally.

9.3.2 Passing a Class Type Argument by Value of Object Reference

As this example illustrates, passing a class type argument by value in Java is different from passing a class type argument by value in C++.

The example first creates a User object in line (A) of main of the Test class. The variable u, which holds a reference to the object constructed in line (A), is the argument of the function call in line (B).

 
//PassClassTypeByValue.java class User { String name; int age; User(String nam, int yy) { name = nam; age = yy; } }; class Test { public static void main(String [] args) { User u = new User( "Xeno", 89); //(A) g(u); //(B) System.out.println(u.name + " " + u.age); // Yuki 200 } static void g(User v) { //(C) v.name = "Yuki"; //(D) v.age = 200; //(E) } }

What the local variable v in line (C) gets is a copy of the object reference held by u in main. So we say that an argument is passed by value of object reference. Recall that a Java object reference can be thought of as a disguised pointer. So the situation here is similar to the case of passing a class type argument by pointer in C++.

For the example code shown above, since the object reference held by the local variable v inside g is a copy of the object reference held by u inside main, both u and v are pointing to the same block of memory that contains the User object created in line (A). Therefore, the changes made by g in lines (D) and (E) will alter the data members of the User object u of main. Hence, the print statement in main will display the state of the object as changed by g.

We said that the pass-argument-by-value-of-object-reference mode in Java is similar to the pass-argument-by-pointer mode in C++. However, a discerning reader might have also noticed a similarity between the above example for Java and the pass-argument-by-reference example for C++. In both cases, the changes brought about by the called function were visible in the calling function. So can we say the mode for argument passing in Java is similar to the pass-by-reference mode in C++? The answer is categorically a No.

We will now show that the argument passing mode in Java does not at all work like the pass-by-reference mode in C++. If the arguments were passed by reference in Java,[4] as opposed to by value, then the call to swap in line (C) of the Java program below would actually cause the object references held by the two arguments to get swapped. But that does not happen.

 
//Swap.java class User { String name; int age; User(String nm, int a) {name=nm; age=a;} } class Test { public static void main(String[] args) { User u1 = new User("Xeno", 95); //(A) User u2 = new User("Yuki", 98); //(B) swap(u1, u2); //(C) System.out.println(u1.name); // Xeno System.out.println(u2.name); // Yuki } static void swap(User s, User t) { //(D) User temp = s; s = t; t = temp; } }

This program simply does not swap the object references assigned to u1 and u2 in lines (A) and (B) above. The function call in line (C) passes the object references held by these two variables by value to the function in line (D). The local variable s acquires a copy of the object reference held by u1 and t gets a copy of the object reference held by u2. While it is true that the references held by s and t get locally swapped inside swap(), the object references held by u1 and u2 inside main() remain unchanged.

In contrast to this behavior of Java, let's consider a program in C++ where the arguments are passed by reference to a swap function:

 
//Swap.cc #include <iostream> #include <string> using namespace std; class User { public: string name; int age; User (string nm, int a) {name=nm; age=a;} }; void swap(User&, User&); int main() { User u1("Xeno", 95); //(A) User u2("Yuki", 98); //(B) swap(u1, u2); //(C) cout << u1.name << endl; // Yuki cout << u2.name << endl; // Xeno return 0; } void swap(User& s, User& t) { //(D) User temp = s; s = t; t = temp; }

When the function swap() is called in line (C) above, the references s and t in line (D) become aliases for the objects u1 and u2 of main. When s and t get swapped in the function swap(), u1 and u2 will also get swapped. We should mention that the same result can also be achieved in C++ by passing pointer arguments if we dereference the pointers in the called function for a wholesale exchange of the contents of the memory pointed to by the pointers, as in lines (E), (F), and (G) in the program shown below:

 
//SwapWithPointer.cc #include <iostream> #include <string> using namespace std; class User { public: string name; int age; User (string nm, int a) {name=nm; age=a;} }; void swap(User*, User*); int main() { User u1("Xeno", 95); //(A) User u2("Yuki", 98); //(B) swap(&u1, &u2); //(C) cout << u1.name << endl; // Yuki cout << u2.name << endl; // Xeno return 0; } void swap(User* s, User* t) { //(D) User temp = *s; //(E) *s = *t; //(F) *t = temp; //(G) }

To summarize the differences between C++ and Java with regard to the different argument passing modes for class-type arguments:

  • To pass an argument by value in C++ means that the parameter of the called function is handed a copy of the argument object in the calling function.

  • On the other hand, to pass an argument by value in Java means that the parameter of the called function is handed a copy of the object reference held by the argument.

  • Said another way, in the pass-by-value mode in Java, the calling function hands the called function a copy of the object reference, but not a copy of the object itself. On the other hand, in the pass-by-value mode in C++, the calling function hands to the called function a copy of the object.

  • To pass an argument by reference in C++—not possible in Java—means that the reference parameter in the called function simply serves as an alias for the argument object in the calling function.

[4]In the manner that arguments are passed by reference in C++, that is by making a local variable in the called function an alias for the argument variable in the calling function.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net