aspose file tools
The moose likes Beginning Java and the fly likes String reference Value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "String reference Value" Watch "String reference Value" New topic
Author

String reference Value

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1220

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

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Oct 19, 2007
Posts: 74
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.


SCJP 1.4, SCWCD 1.4
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1220

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

Joined: Mar 07, 2007
Posts: 1220

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: String reference Value
 
Similar Threads
uestion on pass byobject and pass by value.
unable to sort Object[]
Threads