Win a copy of 97 Things Every Java Programmer Should Know 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

intern() & string concatenation

 
Ranch Hand
Posts: 74
3
  • 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: 23877
142
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: 23877
142
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).
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic