Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many Objects are created ?

 
Thiru Mu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer s1 = new StringBuffer("abc");
StringBuffer s2 = s1;
StringBuffer s3 = new StringBuffer("abc");
How many objects are created ?
 
Ravi Ahuja
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2
 
Thiru Mu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool..this is what i was expecting...
It was given wrong in the question i have refered..
 
Jianghu Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String �abc� is also an "Object".
answer should be 2+1 = 3
If I am wrong, please correct me.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first line creates an object in heap and also adds it to the string constant pool. The second line juss makes s2 point to s1. here s2 will point to the string "abc" in the constant pool and the third line will create a store abc in a new location in the heap.

So in total my guess is "abc" exists at three places 2 in the heap and the other in the string constant pool.

Now my doubt is only about counting the objects.. what should i count 2/3?? my guess is three.

Somebody please reply abt the count and correct me if any mistake !!
 
Thiru Mu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Objects can be created using only 'new'

so no confusion the object cunt here is 2....

correct me if i am wrong..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3.

Strings, and specifically string literals, are handled differently from other objects.

any time you have a new string literal (something in quotes that hasn't been seen before), it is created as a String object in the String Pool.

so, the fact that you have "abc" creates a String object in the pool. having the same literal still gives one object in the pool, so the second "abc" does not create a second String object.

Then, each call of "new StringBuffer()" will give you another object. so, you have the String "abc", and two StringBuffers, each of which has a value of "abc".
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred for clearing doubt.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because of the String constant pool Sun is phasing out questions that ask about how many "String" objects are created - you might get 1 question like this but you probably won't get any.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic