jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Strings" Watch "Strings" New topic
Author

Strings

amarkirt saroay
Ranch Hand

Joined: Mar 16, 2008
Posts: 167
public String testme(){
String s="Fred"; //line 1
s=s+"47"; //line 2
s=s.substring(2,5); //line 3
s=s.toUpperCase(); //line 4
return s.toString();
}
How many string objects will b created when this method is invoked?

Ans was 3.
How ? Are lines 3 and 4 not creating new Strings in the pool?


SCJP-75%
SCWCD-82%
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"amarkirt amarkirt",
Please check your private messages for an important administrative matter.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1405




Line 1: No string will be created, because "Fred" is a compile time literal that will have already been present in the string constant pool.

Line 2: A single new String object will be created for value "Fred47", again "47" itself is a compile time literal (see comment for Line 1).
Total number of new String objects created so far: 1

Line 3: A single new String object will be created by the subString method for value "ed4".
Total number of new String objects created so far: 2

Line 4: A single new String object will be created by the toUpperCase method for value "ED4".
Total number of new String objects created so far: 3

Line 5: The toString method will just return the reference to the String object created at line 4.

Total number of new String objects created per method call: 3


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I don't understand
How come ther is not string created for "Fred"
What do you mean by compilte time literal that will be already present.How do you know it's already present in the string constant pool?

Oh Boy!! This question has been coming up so many times and I am still not clear with it!!!




The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1405

Well, in the previous code snippet the string literal "Fred" will be added to the per-class constant pool by the compiler (compile-time). So before the testme method is called at run-time the string literal "Fred" will already be present in the constant pool, hence no new String object will be created for literal value "Fred". The same is true for the string literal "47".
[ March 20, 2008: Message edited by: Jelle Klap ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Strings
 
Similar Threads
string constant pool from K&B book
How many String are created??
String Garbage Collection
question on GC
Doubt About Strings