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

String Pool and String in Java

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why == will not be printed can anyone help me with this question??Does += return String object or String from pool??

 
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always quote the source where the question comes from.

Also, please tell us the answer/-es you think are correct ones.
 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Please always quote the source where the question comes from.

Also, please tell us the answer/-es you think are correct ones.




I have already mentioned n the question i was thinking correct answer as == & equal
 
Liutauras Vilda
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yogesh doshi wrote:I have already mentioned n the question i was thinking correct answer as == & equal

You haven't quoted the source where the question comes from.

As you know String is an immutable class, += does the string concatenation and creates a new string object at runtime, hence two objects (a == "2cfalse") aren't referring to the same location in memory. Latter one is living in Sting constant pool and it is a compile time constant.
 
Liutauras Vilda
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why you need to quote the source, you can find information here. Important, though.
 
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yogesh doshi wrote:I have already mentioned n the question i was thinking correct answer as == & equal


No, you didn't. You may think that is what your question implied but that is a very subtle nuance and many people will hesitate to jump to any such conclusions. As with anything else related to programming, it helps when you state things explicitly so that your intent is clear.

The Java compiler is pretty good at optimizing code. See my example here: https://coderanch.com/forums/posts/preList/673336/3154766#3154766

In this case, however, the use of a variable and multiple String concatenation statements are enough to make it impractical to try optimize the code.
 
Danger, 10,000 volts, very electic .... 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
    Bookmark Topic Watch Topic
  • New Topic