• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String is not working as i supposed it to work

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,



I read in sierra bate book that to make java more memory efficient,String uses String constant pool that means it will create a single object for two different
references h1 and h2 . so,h1==h2 should return true. but unexpectedly jvm gives false.......why???

I am also confused about the output of line2 and line3 also.so,please explain me this also.
Thanks for reading my post,

regards
Sumit
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its because concat() method creates a new String object by calling one of String class constructors, and you know that if a String is created by calling a constructor then
its not taken from the String literal pool. This explains the output of the above program.

See the source code of String class, and check the concat() method.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string objects h1 = h1.concat("world"), and h2 = h2.concat("world") are not pulled from the literal pool as they create new string objects. Thats why you have an output as false. Of course, all these string objects are on the heap, just the reference to the literal pool is not there.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But even if we don't use concat() but the + operator:

The result is still
true
false
false
false

But if we replace

by


then the result is:
true
true
false
true
(replacing
by

gives true true true true).

I think as inh1 is calculated at runtime a new string is generated.
In h1 is calculated by using a string constant expression. In this case using + means the constant from the pool is taken, using concat() means a new string is generated.

John
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to understand the difference between the concat() function and String concatenation operator +.

1. concat() function:

concat() function uses new String(int offset, int count, char value[]) constructor to create the new concatenated String. (source : java.lang.String source code)

so new String will not be pulled from the String literal pool.

See this:



2. String concatenation operator +

there are two cases here:

2.1. Both the operands are String literals:

If both the operands are String literals then the resulting String object is resolved at compile-time.

From Java Language Specification:


Therefore:


2.2. one or both the operands are reference variables:

If one or both of the operands are reference variables (other than String <<edit: one reference variable must be of String type>>) then at run-time String Conversion takes place.

If one or both of the operands are references of String type, then String conversion is not required for that variable.

Thereafter: "String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method". (Source Javadocs for String class )

So in this case a new String object is created instead of pulled out from the pool.

therefore:
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point.
 
Sam Hazim
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also if you make s2 compile time constant with the final keyword:



It works as a String literal compile time constant, and is considered identical to:



so the results again show true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic