This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String Objects are created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String Objects are created?" Watch "How many String Objects are created?" New topic
Author

How many String Objects are created?

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Problem : Consider the following code and tell how many Objects are created ??




Confusion :- I am confused in answer 5 and 6.

The anonymous string Object created in line 1 and 2 are same or different. ie the anonymous String Object "c";

Thanks !!!
Happy Learning !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Ying Zuo
Greenhorn

Joined: May 31, 2010
Posts: 1
You initialized 5 strings. Line 1 and line 2 are different since they have different length.
Michael Dixon
Greenhorn

Joined: Jan 20, 2010
Posts: 8


The anonymous string object "c" in lines 1 and 2 is the same. The object is a string literal. String literal is placed in the string constant pool, on the heap, by the JVM. Each time a literal is encountered, the pool is checked to determine if it exists. If it exists, it is reused. Otherwise a new one is created and placed in the pool.

The anonymous string object "c" is created at line 1. At line 2, when it is encountered the pool will be checked. Since it exists it will be reused.

BTW, the code will not compile because a } is missing.
harmeet saini
Ranch Hand

Joined: Apr 11, 2010
Posts: 33
Michael Dixon wrote:

The anonymous string object "c" in lines 1 and 2 is the same. The object is a string literal. String literal is placed in the string constant pool, on the heap, by the JVM. Each time a literal is encountered, the pool is checked to determine if it exists. If it exists, it is reused. Otherwise a new one is created and placed in the pool.

The anonymous string object "c" is created at line 1. At line 2, when it is encountered the pool will be checked. Since it exists it will be reused.

BTW, the code will not compile because a } is missing.


So It means 6 String Objects are created, One string object on each line, except for line 1 where two String objects are created( "c" and s3), please correct me if i am wrong

Thanks in advance


Regards,
Harmeet Singh
Varsha Gadekar
Greenhorn

Joined: Dec 14, 2008
Posts: 2
I think 3 String objects will be created.


There is one more similar question :
Given:

How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

I think here it should create 4 objects, but they have given correct answer as C. Can anyone clarify?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Varsha Gadekar wrote:I think here it should create 4 objects, but they have given correct answer as C.

And remember to QuoteYourSources when posting mock questions.
This is one of the most frequently raised questions, please do a search (perhaps for "Fred 47") in the forum, you will find many similar posts & answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String Objects are created?
 
Similar Threads
Question on Garbage Collection
A question from Whizlab
String x = "hi" ; garbage collected ?
Instances of string objects
Confused with String creation.