According to Kathy Sierra & Bert Bates' Book for Sun Java
Certification, in chapter 6th, page 246, portuguese version:
".........String s = "abc" // creates ONE String object and its
reference variable
String s = new String("abc"); // creates TWO String objects and a
reference variable.
//In this case, once we used the "new" keyword, the java language will
create a String Object in a memory, not in the pool, and
//variable "s" is its reference. Furthermore the literal string "abc"
will be inserted in a pool........ "
So, because in the second statemet we have TWO objects, certainly
it'll be using more memory.
On Nov 25, 5:31 pm, Gagan Arora <[email protected]> wrote:
> Agree with Andre. JVM creates a string pool and basically looks/checks up
> the string pool before creating a string literal
> This will be helpful.
>
> http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3
>
> On Wed, Nov 25, 2009 at 4:49 AM, 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- Hide quoted
> >text -
>
> - Show quoted text -
--
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