File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String is not working as i supposed it to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String is not working as i supposed it to work" Watch "String is not working as i supposed it to work" New topic
Author

String is not working as i supposed it to work

Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

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

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
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

Joined: Jul 19, 2011
Posts: 185
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

Joined: Jun 10, 2011
Posts: 207

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

Joined: Jul 19, 2011
Posts: 185
Good point.
Sam Hazim
Greenhorn

Joined: Jul 19, 2011
Posts: 26
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String is not working as i supposed it to work