Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects will be created

 
nisha Murugaiyan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects will be created when the foll line get executed

String str= new String("asdf");
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One, str.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends. Google java string intern
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne, could you be more specific what you mean?
Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Literal strings are cached by the String class. So if you have two lines in your codeonly one String object would be created and both str and str2 would reference this object. You can confirm this is the case by doing
This is known as String interning. There are plenty of references and tutorials around which will give a fuller explanation.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
[QB]Literal strings are cached by the String class. So if you have two lines in your codeonly one String object would be created and both str and str2 would reference this object...

I respectfully disagree. You'll have three String objects on the heap: one referenced by the literal "abcd", and two more as a result of the new String() calls. You can verify this, as (str == str2) will be false, as will (str == "abcd" && str2 == "abcd"). The new Strings you create will not be interned unless you specifically call intern.

Conversely, the following code only uses a single String object, as described:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected.
Thanks for pointing that out. I'd always assumed was just a shortcut for I am now a little wiser.
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Steve, in fact, String s = new String("abc"); creates a new String instance each time it is executed needlessly. So always do String s = "abc" instead.
You can refer to Effective Java by Joshua Bloch, item #4. - Avoid creating duplicate objects.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Morrow:
You'll have three String objects on the heap: one referenced by the literal "abcd", and two more as a result of the new String() calls.


So shouldn't your original answer to the OP have been two ?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So shouldn't your original answer to the OP have been two ?

I haven't addressed the OP, but yes, "String str= new String("asdf");" will result in two separate objects on the heap (as I've hopefully explained).

Cheers.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Mixing you up with David.
Think I'll go to bed now before I get into any more trouble
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Mixing you up with David.
Think I'll go to bed now before I get into any more trouble


No problem. Sweet dreams...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Literal strings are cached by the String class.

This is a common misconception and I'm curious as to its source before it reaches critical mass (unfortunately, many misconceptions/doctrines cannot be stopped).

A String literal is "cached" (better term?) in the constant pool of a class file, just after the magic numbers and what not if I remember erectly. That object is then created (to answer the original question) when that class is loaded by a class loader. In fact, you can load that one class n times with n class loaders and have n different instances of that constant String, which represents that literal.

Anyway, here's my contribution: http://qa.jtiger.org/GetQAndA.action?qids=68&showAnswers=true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic