Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why did String not change?

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I always thought that in Java when you call a method which takes an Object as a parameter you pass a reference to that object to that method. I thought this holds true even for String since it is an object. But the following code shows that I am wrong...



When I run this program, both of my sys outs print blank. So basically my String did not get changed by the stringMutator method. How could this be? Did I not pass that method a pointer to that String?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64607
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, strings are immutable.

Secondly, all your method does is to create a new String object (via the literal), and assign it to the reference variable subject. So you are not making a change to the object that subject is pointing to, but you are changing which object subject is pointing to.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jehan Jaleel wrote:Hi,

I always thought that in Java when you call a method which takes an Object as a parameter you pass a reference to that object to that method. I thought this holds true even for String since it is an object. But the following code shows that I am wrong...


The above statement is right. but,
String is an immutable class, think about it, you will certainly find out why this is..........
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64607
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't actually have any thing to do with the immutability of the String. The same would happen for a mutable object. I just mentioned that because the OP didn't seem to realize it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the fact it is a string is irrelevant here...


Same code, just a different type of object. In the objectMutator method, you create a new MyObject and point the local reference to it. The testObj reference in the testMethod() is unaffected, as is the object it points to.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jehan Jaleel wrote:Hi,

I always thought that in Java when you call a method which takes an Object as a parameter you pass a reference to that object to that method.


That is correct. The caller's value is a reference to the object. That reference value is copied and passed to the method. Now both caller and method have references to the same object. A couple of things worth nothing: 1) A Java reference is much more like a C/c++ pointer than like a C++ reference, so if you're familiar with those concepts, the naming may be confusing, and 2) The parameter passing mechanism described here is that of passing a reference by value, which is just like how C passes pointers by value, and not at all the same as passing an object by reference (though it kind of looks similar if you squint and ignore certain definitions from Java and CS).

I thought this holds true even for String since it is an object.


It does. All parameters--primitives, references, and null--are passed identically in Java. A copy is made of the caller's value and that copy is given to the method.

But the following code shows that I am wrong...


Correct. But not in the way you think you're wrong. You're correct in what you stated above. Where you're mistaken is that you seem to think that Java "pass objects by reference." It does not. Caller and method each have their own variables, both pointing at the same object. Assigning a new value to the method's variable (telling it to point to a different object) does not have any affect on the caller, because the parameter was passed by value.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Firstly, strings are immutable.


True, but not relevant here. The same code structure with a mutable object would have produced the same results.

 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Java passes references by value. Which means that when you pass a reference into a method it's possible for the object it's pointing to be be modified by the method, but it's never possible for the original reference to be pointing at a new object when the method returns.

So:
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It doesn't actually have any thing to do with the immutability of the String. The same would happen for a mutable object. I just mentioned that because the OP didn't seem to realize it.


For a moment there, you previous post had me thoroughly confused!
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the explanation. I am glad that I (and hopefully others) learned something and we sparked some interesting discussion.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jehan Jaleel wrote:Thanks all for the explanation.


You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic