• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

problem with assigning Strings to variables

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why doesnt line 2 give "true"?I mean after line 1 doesn't s3 refer to the same string literal "abc" in String constant pool that s1 is refering to?
Or is there a different mechanism how a new String is created when "+" operator is used.Please explain.
thanks
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because will create a new String object, means s1 and s3 are referring to two different objects.
 
Ravinderjit Singh
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Link might help you.
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was trying out your code because I'm puzzled myself.

When I ran your code and the output is

false
true

However, I changed the line 2 to System.out.println(s1.equals(s3)):



Now the output is

true
true

On the other hand, I'm not sure why s1==s4 outputs to true; my understanding is that String s1, s2, s3 and s4 refer to different objects on the heap - then again, I haven't read the chapter on Strings yet...

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravinderjit Singh wrote:Because will create a new String object, means s1 and s3 are referring to two different objects.


you mean

String s3=s2+"c";


is like

rather than

is it?
thanks
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string pool mechanism is at work here. To conserve memory, the compiler will
at times, share a string object if sX.equals(sY). Apparently in your case it put "abc" in
the pool and used it again. (Maybe for s1, s3 and s4).)

Jim ... ...
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:Hello,



hello
go to the link given by Ravinderjit
it explains what is special about Strings
thanks for replying
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:The string pool mechanism is at work here. To conserve memory, the compiler will
at times, share a string object if sX.equals(sY). Apparently in your case it put "abc" in
the pool and used it again. (Maybe for s1, s3 and s4).)

Jim ... ...


but it seems as if "abc" is not shared here since "s1==s3" turns out to be false.
thanks
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but it seems as if "abc" is not shared here since "s1==s3" turns out to be false.



From reading the link, my understanding is that if a string is concatenated, it will create a new object. However, if you apply intern(), then it looks for pre-existing String object in memory for s3 to point to. The pre-existing object that s3 originally pointed to is now eligible for garbage collection:



Output:

false
true
After Intern Function on s3:
true
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot sandra
reply
    Bookmark Topic Watch Topic
  • New Topic