• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 3851
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic