Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Doubt in String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt in String class " Watch "Doubt in String class " New topic
Author

Doubt in String class

Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57



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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

I think this will be useful: CallByReferenceVsCallByValue


Mohamed Sanaulla | My Blog
Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57
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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: Doubt in String class