• 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

string references and values

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anybody tell what is the difference between the following two sets of statements regarding references and values?

set 1:
String s1 = "abc";
String s2 = "abc";
set 2:
String s3 = "abc";
String s4 = new String("abc");

According to my understanding, references are different and values are same
in both the sets. what do you say?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
set 1:
String s1 = "abc";
String s2 = "abc";

In this case , one object ( "abc" ) is getting created & that is on String Literal Pool & two reference s1 & s2 both are pointing to this object ...
In this case , s1==s2 & s1.equals(s2) both will return true ...

set 2:
String s3 = "abc";
String s4 = new String("abc");

In this case , one object ( "abc" ) is getting created on SLP & one is on Garbage Collection Heap ( that is internally referring to "abc" ) & s3 is directly pointing ( referring ) to "abc" & s4 is pointing to object that is on GCH ...

So in this case , s3==s4 will return false & s3.equals(s4) will return true ...

--------------------

But some peoples say that SLP contains only references not objects , I am not sure about this ...
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I understand. Thanks for your clear explanation.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

How SLP & one is on Garbage Collection Heap are differ from each other, Can u explain a little inbrief.

thanks in advance


With Regards,
Prakash
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic