• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String objects are created, Different authors different opinions ?

 
Kaxhif Khan
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am appearing for OCJA exam and preparing from Mala Gupta and Kethy and Bert books which i found extreemly useful however i do find few things where both authors has slight disagreement which is confusing me..

How many String objects will be created at the end of the following code ?


K&B said in their book (page 265, topic = "Creating New Strings") that 'line A' will create a new object and in addition will place the literal "abc" in constant pool. If an identical string already exist in pool reference is directed toward it instead of creating a new string which means that only "one object will be created" at the end of line B.
According to Mala Gupta, using new String() create a fresh string object but nothing is place in constant pool and only line B will result in placing a string object in string pool. So if i solve according to Mala's the answer is "two objects".

Kindly tell me how many objects will be created and does String str = new String("abc"); will place anything in constant pool ? Thanks in advance
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article explains very well what's happening in both use cases.

Have a read and afterwards simply post your questions/doubts (if any) in this thread.
 
Kaxhif Khan
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey, great it solved my query .. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic