• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String is not working as i supposed it to work

 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic