• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Strings

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x and y are String references and if x.equals(y) is true ,then x==y is true

its always true, right.

as while giving references it first checks in the pool for the same string if its ter ,it refers to the same String.

In K & B, chapter 6, Q.No.19 they have explained thats it always need not be TRUE

how come???//
can anyone explain
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x and y are String references and if x.equals(y) is true ,then x==y is is not necessarily always true
For eg consider

String s1 = "hi"; //interned
String s2 = new String("hi");
String s3 = "hi" //interned

Here
s1.equals(s2) is true
s1 == s2 is false

But while both these tests r conducted on s1 & s3 they will return true. Here s1 & s3 r interned while s2 is new String object.

I think this will clear u r doubt
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
When the string is created using the String object constructed then string is created on the heap.But when the string is created using "",then the string is created in the pool.

So when you compare using the equals it always compare the contents of the string instead of comparing the string reference address.

Here is a small snippet to make you dance with the string objects on heap and strings created in the pool:

String s1= "dance";
String s2= new String("dance");
String s3= "dance";
here s1==s3 will always return true beacause both of them are created in the string pool and share the same reference address.
Suppose if you compare s1==s2. This will not return true as s1 is created in the pool and s2 is on the heap as object.So both of them does not share the same address.
But s1.equals(s2) will return true as equals compares the contents of both the variables.

Hope i made you dance with pool girl and heap girl???
please let me know if you still not clear about the explanation.
Thanks,
Somasekar.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heyhey thanks somashekhar,

now got to know better abt the pool and the heap girls
 
Shiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic