• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a confused concept

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wonderful explanation! I got it.
Thanks Jyotsna!
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic