• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in String class

 
Jatin sachdev
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Output:
AA AA BB BB
s1= AA s2 = BB

if everything in java is pass by value and when i passed s1 as argument then the reference of s1 gets copied as s1 stores the reference of string.
As i am doing s1=s1+" "+s1 then it should have changed the original string but it is not....

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your concept about pass-by-value is incorrect. Pass-by-value has nothing to do with reference. So when you are passing "s1" and "s2" their value gets passed to newly created variables "s1" and "s2" which you have written in the method definition. So "s1" and "s2" which you are passing and "s1" and "s2" in method definition are total 4 different variables.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this will be useful: CallByReferenceVsCallByValue
 
Jatin sachdev
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply...

but let me clarify what i am trying to say:

Since String is a class therefore In this String s1="Hello"
s1 contains reference to the object.

Now if i pass s1 to a function for e.g

func1(s1) //calling func1

void func1(String xyz)

then xyz gets copy of whatever s1 contains.
and s1 contains reference to the object having "Hello"

then if i make changes in the object through xyz then it must reflect in the original object when accessed from s1.
But it is not showing up that....
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jatin sachdev wrote:As i am doing s1=s1+" "+s1 then it should have changed the original string but it is not....

Why should it have changed the original String? Strings are immutable and cannot be changed. What you're doing in that method is creating new Strings that you assign to the local parameter variables s1 and s2. Afterwards, these two no longer refer to the original Strings but to the new Strings. The original Strings, with references in the original s1 and s2 local variables of the main method, remain as they were.

If you use StringBuilder instead of String, this allows you to modify the actual object instead of only the reference. It will then work as you expected:
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jatin sachdev wrote:

Note that you should never create String objects like this. It's unnecessary to create a new String object from a string literal. Just do this:
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here in the main method s1 and s2 are just like keys to the String Builder objects(Say locks). Now passing by value means you make duplicate keys to open and close those locks. now you have 2 keys for each stringbuilder object. making s1 = null and s2 = null in the getString(StringBuilder...objects) method is like losing the duplicate keys but you still have another pair of keys to work with the locks.


Strings are immutable. Their state cannot be modified.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jatin sachdev wrote:then if i make changes in the object through xyz then it must reflect in the original object when accessed from s1

While this is a true statement, it does not apply here.

When you do this:

you create (several) brand new String objects, each with their own memory address. One of these new ones then gets assigned to s1, but the original is not ever changed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic