This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

intern() & string concatenation

 
Greenhorn
Posts: 26
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What does string concatenation return? Does it return from string pool or return a new String object?
I know when two string literals concatenates, it is in string pool.





Thanks,
Henry
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

henry leu wrote:
What does string concatenation return? Does it return from string pool or return a new String object?
I know when two string literals concatenates, it is in string pool.



If the operands are compile time constants, then the concatenation is done by the compiler, and the results is in the string pool. Otherwise, it is done at runtime, and the result is a new object.

.... and of course, this is implementation detail, so, may change in the future.

Henry
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And as for the intern() method, that throws a monkey wrench into this question (somewhat). The method call will put the string parameter into the string pool.

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

henry leu wrote:Line 4:  System.out.println(output == "HELLOWORLD"); // prints false, WHY?


Because the string to which output is referring to is created at runtime and not in the string literal pool and therefore it's different from the string literal "HELLOWORLD".

henry leu wrote:// I think this convert the entire string  into string pool


You are absolutely spot-on! And your thought was already confirmed by the output of the comparison using the == operator on line4.

Hope it helps!
Kind regards,
Roel

Disclaimer: the intern() method is not on the OCAJP exam (it isn't even on the OCPJP exam).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!