Ok, I found some more information.  Java attempts to reduce the usage of
memory and and CPU time by doing what is called 'interning' of Strings.
 Basically with interning, the JVM keeps a single copy of identical Strings.
 All String literals are interned at compile time.  However, Strings created
at runtime using the new keyword will not be interned.  Therefore:

String str = "Hello" will benefit from interning and save memory,

but

String str = new String("Hello") will not benefit from intening and will
create a brand new object even if identical strings exist.

See http://mindprod.com/jgloss/interned.html for more details.

On Wed, Nov 25, 2009 at 13:03, Andre Brown <[email protected]> wrote:

> Both statements accomplish the same thing - they both create a string
> object.  The first statement creates the string by presenting a String
> Literal.  When the complier encounters a String Literal in the code, it
> creates a String Object with the value presented in the quotes.  See -
> http://java.sun.com/docs/books/tutorial/java/data/strings.html. Based on
> the foregoing, I don't believe there is a difference in memory usage.  I'll
> keep looking into it to see if there is anything I'm missing.
>
>
> On Wed, Nov 25, 2009 at 07:49, analyn flores <[email protected]>wrote:
>
>> Which saves more memory?
>>
>> String str = "";
>> or
>> String str = new String();
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]<javaprogrammingwithpassion%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
>
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to