wood burning stoves*
The moose likes Java in General and the fly likes How many objects will be created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How many objects will be created" Watch "How many objects will be created" New topic
Author

How many objects will be created

nisha Murugaiyan
Greenhorn

Joined: Aug 18, 2005
Posts: 1
How many objects will be created when the foll line get executed

String str= new String("asdf");
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
One, str.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
It depends. Google java string intern


Joanne
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Joanne, could you be more specific what you mean?
Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
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

Joined: May 22, 2003
Posts: 657

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

Joined: Aug 05, 2005
Posts: 3419
    
  12
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

Joined: Aug 10, 2005
Posts: 47
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

Joined: Aug 05, 2005
Posts: 3419
    
  12
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

Joined: May 22, 2003
Posts: 657

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

Joined: Aug 05, 2005
Posts: 3419
    
  12
Sorry. Mixing you up with David.
Think I'll go to bed now before I get into any more trouble
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 24, 2003
Posts: 1608

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


Tony Morris
Java Q&A (FAQ, Trivia)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How many objects will be created
 
Similar Threads
String objects
How many objects are created?
String
How many objects
String and String Object Problem