• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in String class

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this will be useful: CallByReferenceVsCallByValue
 
Jatin sachdev
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic