Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Constructor

 
michael aron
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String constructor is still called, so why is that more efficient?
 
Val Dra
Ranch Hand
Posts: 439
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
michael aron
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it! Thanks, everyone...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic