• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

string confusion

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ragardin strings:

class s11
{
public static void main(String[] args)
{

String s11 = new String("H");
String s12 = s11;

System.out.println(s11==s12);
}
}

the output is true.
shldnt it be false.
s11 creates a new string object with new.
and s12 is assigned s11.
but s12 is in the string pool and s11 is a new string
so how does this result true using ==.
pls clear this
thx
regards
Kamal
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
h is the only object in memory whose reference has been assigned in two direction's for eg
ss2-------->ss1--------->"h"

ss2 is pointing the same object and ss1 is also pointing the same object.They have same reference's in our case.
rememeber == compares the reference's if they are same it give's true otherwise false
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kamal jaisingh,
When u create a String with a new operator u actaually create a String object.
That is your
String s11 = new String("H");
Here s11 is object reference which holds a pointer to H.
Now in
String s12 = s11;
we have assigned the object reference s11 to s12. In other words after this operation both s11 and s12 will point to "H".
Now equality operator == will be checking for the object reference equality and in this case it is.. Hence the answer.
Correct me if I am wrong.
 
kamal jaisingh
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx a lot for ur replies
now its clear to me abt string and object references.
thx again
regards
Kamal J
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic