• Post Reply Bookmark Topic Watch Topic
  • New Topic

String reference Value  RSS feed

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How in the below code String value alone is not modified by modString method.




Output:

Before calling mod method: 5
Before calling mod method: ABC : 1
Array Value: 10
String Value: XYZ : 2
After calling mod method: 10
After calling mod method: ABC : 2
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishnu!

Your string variable is simply not modified within the method modifyString(...) because Java ALWAYS uses pass-by-value! So if you pass in a string reference to your method the reference itself is copied. And in your method you assign this copy of the original reference another String object but the original String reference won't be touched!

Regarding your other references for example the int array abcArray[]: Here you're modifying the object itself the reference points to (abcArray[0] = 10) but you're not assigning it a completely new object!

I hope the difference between your reference manipulations is now a bit clearer. You're dealing with refernce parameters only but you're using them in a different way within your modifying methods If it's not clear just let me know...

Marco
[ October 23, 2007: Message edited by: Marco Ehrentreich ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear with your response.

Here is my understanding,

We are getting the below results as a result of immutability feature of Strings.

Am I correct here?
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the String object in Java is immutable. It has no methods to modify it.


will just create a new String object and point s to the new object.

Use a StringBuffer or StringBuilder if you want a "modifiable" string.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that "simple" Strings in Java are immutable but this has nothing to do with object reference parameters. The problem Vishnu encountered is the same with ALL object reference types in Java. I'll try to explain it once more...

You have a reference variable to a String object:

String a = "hello";

And you pass this reference variable to your method:

modifyString(a);

If you now use the "a" reference in your method you actually use a local copy of the reference you passed to the method. When you assign "a" a new String object IN the method only the local copy of "a" is affected!!! After your method returns the original "a" reference still points to the String "hello". But this only affects the problem of assigning a new object to your reference parameter you pass to the method. You simply can't do this in Java because Java doesn't use pass-by-reference. It's always pass-by-value regardless if your parameter is a reference type or a primitive data type like "int".

The fact that usual String objects are immutable in Java just means that you can't manipulate the original String object your reference points to.

I think your problem in understanding this is simply that you're mistake manipulating the reference variable itself with manipulation the object (e.g. String) the reference points to!

Sorry for my bad English but i hope now it's clearer... If it still isn't just let me know

Marco
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to explain you the problem again with your modifyString() method


public static void modifyString(String abc, TestA a) {

abc = "XYZ";
a.x = 2;
System.out.println("String Value: " + abc + " : " + a.x);

}


With
abc = "XYZ";
you're assigning a new String object to the local String reference "abc". Note that this is a local copy your "s" reference variable you pass to the method as argument. This means that only the local reference is changed and the original reference "s" still points to the String "ABC"!!

The line
a.x = 2;
is a little bit different. In this case you're not manipulating the reference "a" itself. Instead you're manipulating the object of type TestA the reference points to. And this TestA object is the original object so the value of "x" will be changed even if the method returns.

Do you now understand the difference between the two?

Marco
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!