[ajug-members] creating strings

Joe Sam Shirah joe_sam at bellsouth.net
Sun Jul 22 13:49:48 EDT 2007


    Hi Sony,

    "intern" is the technical term.   All literal strings and string-valued
constant expressions are interned, as indicated by Ashish's mail below.
Explicit "new" Strings do not participate unless String.intern() is called.
See:

http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern()

which also points you to the appropriate JLS section.


                                                         Joe Sam

Joe Sam Shirah -        http://www.conceptgo.com
conceptGO       -        Consulting/Development/Outsourcing
Java Filter Forum:       http://www.ibm.com/developerworks/java/
Just the JDBC FAQs: http://www.jguru.com/faq/JDBC
Going International?    http://www.jguru.com/faq/I18N
Que Java400?            http://www.jguru.com/faq/Java400



----- Original Message ----- 
From: "ashish butani" <abutani at hotmail.com>
To: "General AJUG membership forum (100-200 messages/month)"
<ajug-members at ajug.org>
Sent: Sunday, July 22, 2007 1:16 PM
Subject: RE: [ajug-members] creating strings



I think Strings are pooled by JVM for improving efficiency. By explicitly
creating objects using the new operator you are indicating to the JVM to not
use it from the pool.

For example if you have something like this

String a = "xyz";
String b = "xyz";
String c = new String("xyz");

In the above case, a new object is created for a and c but not for b. This
is because b references to the same literal as a (i.e xyz).

Thanks
Ashish Butani





> Date: Sat, 21 Jul 2007 22:54:34 -0400> From: sony.antony at gmail.com> To:
> ajug-members at ajug.org> Subject: [ajug-members] creating strings> > Is
> there any different between teh following string instantiations at> run
> time ( memory, speed of execution etc. )> > String s = new String( "XXX")
> ;> > String s = "XXX" ;> > > I thought they are both exactly same as new
> strings are created only> if there isn't already an entry in the string
> table for the same> string.> > > --sony




More information about the ajug-members mailing list