• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

String in Pool or Heap  RSS feed

 
Ranch Hand
Posts: 227
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output
str5 =Hello
str8 =Hel
str10 =Hello
str10==hello? false
str5==hello?true
str10==str5?false



Why does the str10 result as false ? Line no : 11
 
Marshal
Posts: 59762
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and?
The String expression produced by the + operator in line 7 isn't a compile time constant.
 
Saloon Keeper
Posts: 935
32
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Santosh Kumar Nayak wrote:Why does the str10 result as false ? Line no : 11

Because on line no 7 here this is not a constant expression so String concatenation is performed where

JLS 9 15.18.1. String Concatenation Operator + wrote:The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).

ergo new object is created although same content String present in String constant pool (which is created by constant expression on line 3 and referenced by hello reference variable ) so str10 has different reference than hello.
If you compare then It will return true because both have String values of constant expressions which are interned.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 227
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So



Produces an object at runtime and it is not in String Pool
 
Ganesh Patekar
Saloon Keeper
Posts: 935
32
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Santosh Kumar Nayak wrote:So Produces an object at runtime and it is not in String Pool

Yes It produces a new String object having value "Hello". If you declare str8 and str9 final then becomes compile time constant and reference of String "Hello" from String pool is returned so str10 == hello and str10 == str5 will return true. I had tried different combinations and also drawn an illustration for that, will see If I find in my documentation will post soon.
 
Ganesh Patekar
Saloon Keeper
Posts: 935
32
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I had tried with different combinations also had drawn illustration for that. Please bear my hand writing.   Output:
one == two: false
one == five: false
one == six: true
one == eight: true
one == ten: false
one == twelve: true
An-Illustration.jpg
[Thumbnail for An-Illustration.jpg]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!