aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many string objects created?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "How many string objects created??" Watch "How many string objects created??" New topic
Author

How many string objects created??

Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
String s = new String("abc");

How many string objects exists after this line of code executed?

Two? One String object on the heap and one in String constant pool?

Or just one?

Really confused here.... Pls help...


SCJP 5.0 - 98% (2007)<br />SCWCD 1.4 - 97% (2007)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Two strings. To keep things simple, count string literals once only and count each occurrence of "new String".

In your example: one for "abc" and one for new String("abc"), that gives two strings.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
Originally posted by Barry Gaunt:
count string literals once...

Is it "count string literals once" or "count string literals once for each class" ?
In other words, does the same string literal occuring in two different class files end up at the same place in the string pool?
(Or is it completely implementation dependent?)
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
I guess Barry Gaunt's answer only applies for SCJP exam...
Govinda Reddy
Greenhorn

Joined: Apr 07, 2006
Posts: 7
one reference(s)
two objects one for "abc" and one for new String("abc")
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Jon Lee:
I guess Barry Gaunt's answer only applies for SCJP exam...


Yes, that's why I said "to keep things simple" meaning for the examples you will see in the SCJP exam.

In real situations you have to consider the possible used of the intern() method, and the possibility of using different class loaders.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Neelesh Bodas:

Is it "count string literals once" or "count string literals once for each class" ?
In other words, does the same string literal occuring in two different class files end up at the same place in the string pool?
(Or is it completely implementation dependent?)


I just tried an example with static string literals in two different classes in two different packages and the two strings compared with "==" as true.
So it seems plausable (but not proved).

Yes, it also works with non-static literals in different classes in different packages. So it still seems plausible(but not proved).

If you mention class loaders I will move this thread to Java In General (Advanced)
[ July 26, 2006: Message edited by: Barry Gaunt ]
 
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: How many string objects created??
 
Similar Threads
How many objects will create
Strings in Java
String Object Creation
String Doubt
String and String Object Problem