This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Constructor" Watch "String Constructor" New topic
Author

String Constructor

michael aron
Greenhorn

Joined: Jan 30, 2001
Posts: 5
I read that when you use the shortcut:
String sString = "bla blah blah";
that the String constructor is called... if this is the case would it be more efficient to code:
String sString = new String("blah blah blah");
Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The String constructor is still called, so why is that more efficient?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
String sString = new String("blah blah blah");
actually it won't be sufficient because the object will be disgarded which is not what you need , it's an extra step which isn't needed


Val SCJP <BR>going for SCJD
michael aron
Greenhorn

Joined: Jan 30, 2001
Posts: 5
I'm not understanding, I figured if you explicitly call the constructor it would be more efficient than having Java say "oh yea I need to call the String constructor now".
Asuthosh Borikar
Ranch Hand

Joined: Sep 29, 2000
Posts: 75
I think this has to do with the JVM Run Time pool of Strings, rather than the time it takes for Java to figure out that it needs to call the constructor.
When you say
String sString = new String("blahblah");
you will invariably create a new object and we want to avoid creating new objects if we can.

But, when you say
String sString = "blahblah";
java will first look in the runtime pool of Strings and create a new object only if a String with the value "blahblah" hasn't already been created.
To summarize,
String sString = new String("blahblah");
String tString = new String("blahblah");
will ALWAYS result in two new objects.
But,
String sString = "blahblah";
String tString = "blahblah";
will only result in one new object.
Hope I was clear.
Susan Hoover
Ranch Hand

Joined: Jan 04, 2001
Posts: 64
Originally posted by Asuthosh Borikar:
To summarize,
String sString = new String("blahblah");
String tString = new String("blahblah");
will ALWAYS result in two new objects.
But,
String sString = "blahblah";
String tString = "blahblah";
will only result in one new object.

Actually,
String sString = new String("blahblah");
String tString = new String("blahblah");
could potentially result in not TWO but THREE new objects. The first object is the string literal "blahblah" which, as you pointed out, will exist in the pool of literal strings after this call is made. The other two are the new Strings that are explicitly created.
(reference: RHE 2nd ed. p. 257)
Either way, to the original question,
String sString = new String("blahblah");
is very wasteful.
--
Susan

[This message has been edited by Susan Hoover (edited January 30, 2001).]
michael aron
Greenhorn

Joined: Jan 30, 2001
Posts: 5
got it! Thanks, everyone...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Constructor
 
Similar Threads
Identifier Expected
Campaign for deletion of "Word Association" thread.
<<<EOF in javascript?
Panel vs Frame
trouble in using connection pooling in tomcat