• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String Objects are created?

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!

 
Ying Zuo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You initialized 5 strings. Line 1 and line 2 are different since they have different length.
 
Michael Dixon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Varsha Gadekar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic