• 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

"==" and equals()

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I know that operator "==" compares references and equals() values, but, I know too that String is imutable, so why this code on the first example gives 'true' if the others gives 'false'?


 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bruno,
Strings are stored in a pool of String so when you do
String a = "a";
String b = "a";
variable a creates a new string in a pool and variabe b also points to the same String "a" in that pool and this == gives true for that. When you have something like
String c = new String("a");
Thats means you want to create a whole new string and not reference it to the same string created before, in this case it will give false.
Hope this helps. Someone will correct it if I am wrong, I am giving my certification on 19th of this month.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct,
think about a swimming pool, 25 people swimming in it and 5 people are called colin.
say someone puts a call over a tannoy for colin, which one responds?
there is only one name colin but 5 people have that name, this is like the string a = "a"; scenario, so:
string body1 = "Colin";
string body2 = "Colin";
string body3 = "Colin";
string body4 = "Colin";
string body5 = "Colin"; //each person has used the same name.
but string bodynew = new string("Colin"); gets its own address, so it is not in the pool, and therefore bodynew and body1 will not be the same, but body1 and body2 will be.
Hope this helps you see it in a differenet way.
Davy
[ March 10, 2004: Message edited by: Davy Kelly ]
 
Bruno Frascino
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks buddies!
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic