aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a confused concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a confused concept" Watch "a confused concept" New topic
Author

a confused concept

Anon Ning
Greenhorn

Joined: Mar 09, 2001
Posts: 19
hi all:
See this code below:
public class TestClass17
{
static String str="Hello World";
public static void changeIt(String s)
{
s = "Good bye world";
}
public static void main(String[] args)
{
changeIt(str);
System.out.println(str);
}
}
The output is :Hello World
Why the output is "Hello World"? I think 'str' is a reference and after it was passed to method changeIt(String s),its value will be changed to "Good bye world".So the output should be: Good bye world. But it isn't.I was confused .Please help me.
Thanks!
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158

In java all parameters arepassed by value regardless of whether they are
primitives or objects.
For primitive pass by value means a copy of the primitive is passed.
For Objects passed by value means a copy of the reference to that object is passed.
Remember
A COPY of the reference to the object indicates that there are TWO references to the (single) object
String str ="Hello World"
creates one reference to the string.
This refernce is str
Now you call method changeIt(str) which will pass a copy of the reference (str) but NOT a copy of the object itself.

Here in the method the copy of the reference is s .
So now you have 2 references to "Hello World" as both str and s refer to this String.
However within the method reference s is changed to refer to "Good bye World" while str is UNTOUCHED !!
That's why when you call System.out.println(str) in main() it prints "Hello World"
Anon Ning
Greenhorn

Joined: Mar 09, 2001
Posts: 19
Thanks Jyotsna.
But how about 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?
Please expalin me .Thanks!
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
OK Let's take a look a this:
Remember a reference is simply a pointer (or address).
This reference may point to a primitive variable or it may point to an object.

First is same explanation as previous post- I'll just repeat it
The method call incr(i), i is passed by value
n is the copy of the SAME reference .
Now n is changed to 2. That means that the reference n NOW points to a value 2.
BUT reference i still points to 1.
Now the second method is a little tricky
Here we pass an array object reference iArr by value So reference iArr and reference n point to the SAME array object{1}
Now n[0] is incremented so the array object is {2}.
Note: n has now manipulated the object that it was pointing to. This
However, iArr is ALSO pointing to the SAME object array
iArr is still referencing the SAME array but the first element was changed by the method.
So we see that
1. incr(i) passes the refernce of a primitive When you change the reference of n it points to a new value. You cannot change the reference value itself.
2. incr(iArr) passes the reference to an OBJECT. So you can use the reference to change the contents of the object.

Though you always pass parameters by value (or a copy of the reference) for both primitives and for objects,
you cannot USE the reference to change the data in the primitive variable
but you CAN USE the reference change the contents(data) within an object
This is the difference between passing primitives and passing objects (an array is an object)
[This message has been edited by Jyotsna Clarkin (edited March 15, 2001).]
Anon Ning
Greenhorn

Joined: Mar 09, 2001
Posts: 19
Wonderful explanation! I got it.
Thanks Jyotsna!
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Jyotsna,
In the first example you said that "str is UNTOUCHED !!". Why wouldn't iArr be untouched also if they are both passing a copy of the reference to an object?
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
Originally posted by Ronnie Phelps:
Jyotsna,
In the first example you said that "str is UNTOUCHED !!". Why wouldn't iArr be untouched also if they are both passing a copy of the reference to an object?

Hi Ronnie-
iArr is 'untouched', so to speak, because it ALWYAS points to the SAME array object.
The contents of said array were changed by n, which was another reference (the copy) local to the method incr(iArr).
And now iArr will reflect that change because the object itself has changed in some way.
iArr cannot be made to point to anything else simply by changing the local reference in the method.
That's what I said in my previous post about the difference in changing values for primitive & for objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a confused concept
 
Similar Threads
Object Reference Question
Passing Arguments
Pass By Reference
Pass by reference
pass by value