aspose file tools*
The moose likes Java in General and the fly likes Why did String not change? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why did String not change?" Watch "Why did String not change?" New topic
Author

Why did String not change?

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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

Joined: Jan 10, 2002
Posts: 61420
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

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

Joined: Jan 10, 2002
Posts: 61420
    
  67

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

Joined: Oct 02, 2003
Posts: 11406
    
  16

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 06, 2010
Posts: 4421
    
    8

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

Joined: Jul 03, 2011
Posts: 256
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

Joined: Apr 30, 2002
Posts: 196
Thanks all for the explanation. I am glad that I (and hopefully others) learned something and we sparked some interesting discussion.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jehan Jaleel wrote:Thanks all for the explanation.


You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why did String not change?