wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pass by value" Watch "pass by value" New topic

pass by value

Anon Ning

Joined: Mar 09, 2001
Posts: 19
hi all;
Look the following code:
public class TestClass10
public static void main(String args[ ] )
int i = 1;
int[] iArr = {1};

incr(i) ;
incr(iArr) ;
System.out.println( "i = " + i + " iArr[0] = " + iArr[0]) ;
public static void incr(int n ) { n++ ; }
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
The output is :
i=1 iArr[0]=2
I think the output should be: i=1 iArr[0]=1
why iArr[0] is 2?
I feel confused about Pass by Value.Can someone explain this concept to me?
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73

This is because of pass by value and pass by reference. All objects created are passed by the reference. All primitive datatypes are passed by value. Now remember that String and array are objects and they have methods and properties. We sometimes think that an array of primitives like int is primitive. But an array, even if it is an array of int, byte, or float is an Object and is passed by reference.
Now see this code:
public static void incr(int n ) { n++ ; }
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
The reference (a pointer) to an array object is what we get in n in the method incr(int[] n). Hence an update of element (property) of the Array(object) will modify the original array.
-- Hope this helps

Hari Gangadharan
Unix is user friendly..
but it chooses to whom it is friendly with!

<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
Christopher Gregory

Joined: Feb 08, 2001
Posts: 4
Hey Anon! In Java arrays are objects, so anytime you pass an array into a method you are passing a reference to that array into the method. On the otherhand when you pass a primitive data type into a method you are passing a copy of that primitive data value into the method. So take the first of your two methods:
public static void incr(int n ) { n++ ; }
the parameter n is a primitive data type so pass by value will be used. So when you call it with this statement (incr(i) a copy of i is sent to the method and inside method incr that copy is modified. When the method returns that copy that has been modified in the method is destroyed and you are left with the original i which hasn't been changed.
With your second method you are passing an array which is an object, thus, the array is passed by reference.
public static void incr(int[ ] n ) { n [ 0 ]++ ; }
When you call this method with the statement (incr(iArr) you are passing a reference to your array into the method. So in other words, no copy of the array is constructed. Consequently, when the statement (n[0]++) is executed you are modifying the original array defined back in main(String args[]). That is why n[0]= 2
I hope this helps!!
Anon Ning

Joined: Mar 09, 2001
Posts: 19
Thanks Christopher.
But I can not understand this sentence in your explain
"no copy of the array is constructed."
Can you explain it further?
In addition,see the following code:
public class TestClass17
static String str="Hello World";
public static void changeIt(String s)
s = "Good bye world";
public static void main(String[] args)
The output is : Hello World
Why not "Good bye world"? Does it pass a copy of reference "str"?
Please explain it.Thanks.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
A String is an object. A copy of the reference to the String was passed. The method re-set the LOCAL VARIABLE s to another value, and didn't pass anything back. That s died when the method was over.
If you really wanted to change str you would have needed to either 1) reference str directly in the method (str is not a local variable), or 2) create an instance of TestClass17 and use it to access str. (myTestClass17.str = whatever).

Try reading this:

"JavaRanch, where the deer and the Certified play" - David O'Meara
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
Is the reason the original remains the same have to do with the fact that we're passing a ref to a String literal to the method. I don't see any difference between the array example and the String exampleexcept for this. In both examples the local copy of the reference is changed. In one case (the array example) the original object is changed but in the String example it's not.
Naveen Arumugam

Joined: Mar 10, 2001
Posts: 23
In java everything is "pass-by-value" & not pass-by-reference for objects & pass-by-value for primitives.
Whenever , U create an array [primitive/object/interface] its an object.Because, in java classes, interfaces & arrays are considered as reference type.So, when u pass it to a method & change it, it gets reflected.

I agree. Here's the link: http://aspose.com/file-tools
subject: pass by value