• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

string references and values

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Now I understand. Thanks for your clear explanation.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!