• 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

String + operator doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I create a new String object using the + operator, like

String a = "A";
String b = "B";
String c = a + b;
String d = a + b;
System.out.println((a+b) == (a+b)); //returns false WHY??

Anyone please??
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String a = "A";
String b = "B";
String c = a + b;
String d = a + b;
System.out.println((a+b) == (a+b)); //returns false WHY??



Since string objects are immutable, whenever you use + operator on string, it will create a new string. So if you do (a+b) == (a+b), two new string object would be created and the reference would be different. so it will return false..

(a+b).toEquals(a+b) should return true since it's checking string contents.. though it will create two objects too, the contents would be same..
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome Kunal to JavaRanch...good reply..Now coming to the topic
Surprisingly the string pooling comes into the picture once you change the code like

and returns true.

Kunal writes..
(a+b).toEquals(a+b) should return true since it's checking string contents.. though it will create two objects too, the contents would be same..


There is not such method toEquals()in String class.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kunal.

welcome to the Java Ranch!


To Jothi:
one additional remark:
it would print true if both a and b were final.

Bu.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard Hassel,

As you said

it would print true if both a and b were final.



I would like to know why it will print true even if both are final but a+b will create a new string object in the pool.

thanks
Anvi
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bu,

Thanks for the help and I have got the logic behind it.

Thanks fellow ranchers as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic