Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concatenation on int type, is new String created in the pool?

 
Raphale Style
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code below,

int i = 5;
String s = " " + i;

How many Strings are created in the pool?//actually about exercise 9 in chapter 5 of the book OCA/COP JP Study Guide by K.Sierra and B.Bates

My count is: " ", "5", " 5", three.

Am I correct? I'm not sure how the integer is concatenated, is it wrapped into type Integer and then invoked Integer.toString();?

Thank you.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raphale Style wrote:How many Strings are created in the pool?//actually about exercise 9 in chapter 5 of the book OCA/COP JP Study Guide by K.Sierra and B.Bates

My count is: " ", "5", " 5", three.

Am I correct? I'm not sure how the integer is concatenated, is it wrapped into type Integer and then invoked Integer.toString();?

The question in the book asks for the number of objects in memory, not the String literal/constant pool. Please note that only String literals (and compile-time constants) will be added to the String literal/constant pool, not strings created at runtime. This excellent article is about strings, literals and the String literal/constant pool. It's (without any doubt) a must-read for every Java developer and certainly if you are preparing for OCAJP (or OCPJP).

Number of strings in the string literal/constant pool: 1 (only " " is a String literal)
Number of objects created: 2 (" " and " 5")

A primitive is concatenated to a String without first being converted to a String. It's also not wrapped to an Integer object, it's just concatenated.

There is a very detailed discussion (with decompiled code & byte code) in this thread about this particular question from K&B7. Warning: read at your own responsibility (certainly if you new to Java)! It's so far beyond the scope of the OCAJP7 exam, it's even way beyond the scope of the OCPJP7 exam. This thread includes wrong/misleading code in some parts because of confusion through the evolution of the clarifications, so if checking that thread, make sure you go all the way to the end where the actual issue is finally truly clarified.

Hope it helps!
Kind regards,
Roel
 
Raphale Style
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
The question in the book asks for the number of objects in memory, not the String literal/constant pool. Please note that only String literals (and compile-time constants) will be added to the String literal/constant pool, not strings created at runtime. This excellent article is about strings, literals and the String literal/constant pool. It's (without any doubt) a must-read for every Java developer and certainly if you are preparing for OCAJP (or OCPJP).

Number of strings in the string literal/constant pool: 1 (only " " is a String literal)
Number of objects created: 2 (" " and " 5")

A primitive is concatenated to a String without first being converted to a String. It's also not wrapped to an Integer object, it's just concatenated.

There is a very detailed discussion (with decompiled code & byte code) in this thread about this particular question from K&B7. Warning: read at your own responsibility (certainly if you new to Java)! It's so far beyond the scope of the OCAJP7 exam, it's even way beyond the scope of the OCPJP7 exam. This thread includes wrong/misleading code in some parts because of confusion through the evolution of the clarifications, so if checking that thread, make sure you go all the way to the end where the actual issue is finally truly clarified.

Hope it helps!
Kind regards,
Roel


Thank you very much. Now I know that for things like " "+3, the primitive just concatenated without creating extra String.

With best regards,
Raphale
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic