• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many objects will be created using new in string  RSS feed

 
Rj Jha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String a=new String("abc");//Per Kathy Sierra SCJP book, 2 objects will be created. one will be in the heap memory, refrence variable will point to this object. Another objecti.e. string literal will be created in string pool memory without having any refrence variable..it it correct?if yes, how.
String x="abc" //1 object will be created in string pool memory.

Please post answers if you are sure about it. No guess please.

Thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Jha wrote:String a=new String("abc");//Per Kathy Sierra SCJP book, 2 objects will be created.


Only one object is created as a result of executing that line--the one from new String(...). The other String object is created when the class is initialized, not when that line is executed.

one will be in the heap memory, refrence variable will point to this object. Another objecti.e. string literal will be created in string pool memory without having any refrence variable..it it correct?if yes, how.
String x="abc" //1 object will be created in string pool memory.


The String "abc" is in the .class file for that class. When the class is initialized, the JVM looks for all String literals in the class, and creates entries in its constant pool for those that are not already there, and updates the class's constant references to point to the appropriate place in the JVM's constant pool.

Roughly speaking, the class's bytecode says, "Get reference #1 from this class's constant pool ("abc"), use it to create a new String object (new String(...)), and store a reference to that new String in variable a (a = ...). The JVM resolves the "reference #1 from this class's constant pool" to "reference #xyz from the JVM's constant pool."

Please post answers if you are sure about it. No guess please.


This is a public forum, and anybody is free to post whatever answers they want. Trying to dictate the nature of the answer will only serve to alienate people who might otherwise have helped you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Jha wrote:Per Kathy Sierra SCJP book, 2 objects will be created. one will be in the heap memory, refrence variable will point to this object. Another objecti.e. string literal will be created in string pool memory without having any refrence variable..it it correct?

Yes, but that doesn't mean it doesn't have a reference. A String literal is an object just like any other, which is why it's quite legal to write things like
"abc".length()
literals are just cached in the String pool for reuse.

if yes, how.

I don't understand the question. The explanation is what you've already written. The 'new' keyword ALWAYS creates an object, and the second one holds the "abc" literal.

String x="abc" //1 object will be created in string pool memory.

Precisely. No 'new' keyword, no new object. 'x' is simply assigned the reference to the literal "abc".

Winston

Edit: Too slow, as usual :wink:
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Edit: Too slow, as usual

Don't you miss JosAH at times like these?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!