This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Dec 10, 2009 6:44 AM by 843789 RSS

object creation of string

843789 Newbie
Currently Being Moderated
Hello
Please tell me what is the difference between these two object creation of string.
String str = new String("Hello");.
String str="Hello";
Thanks.
  • 1. Re: object creation of string
    796217 Newbie
    Currently Being Moderated
    Autoboxing?
  • 2. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    hsc71 wrote:
    Autoboxing?
    Is this a question or an answer ?

    As per the OP query creating a string object using new creates a new String object ,whereas one without new String corresponds to the String literal being picked from String literal pool if existing or creating a new String and adding it to the pool.

    Such strings are not equal by the == operator,but are equals if checked using equals() method.

    THanks.
  • 3. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    Thanks for your reply,


    Is the string pool is part of a heap? As i understand if i write String s = "hello"; the jvm will create a string literal "hello" in a string pool and s is referring to that literal. and if i write String s1 = new String ("hello"); the jvm will create a new string object in heap and s1 is referring to that object, where the "hello" literal go? please suggest me.

    Thanks.
  • 4. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    RGEO wrote:
    Thanks for your reply,


    Is the string pool is part of a heap? As i understand if i write String s = "hello"; the jvm will create a string literal "hello" in a string pool and s is referring to that literal. and if i write String s1 = new String ("hello"); the jvm will create a new string object in heap and s1 is referring to that object, where the "hello" literal go? please suggest me.

    Thanks.
    " hello " literal created with String s="hello" will continue to exist in the String literal,in case you define another String s3="hello"; it will point to this String literal. In case you modify s3 like,

    s1=s1+"guys",now s1 will not point to "hello" but another string will get created and s1 will now point to that string.

    You can arrange for your new String to be placed in the pool for possible reuse by using the intern() method of the String class.

    Experts may advise on this.

    Thanks.

    Edited by: punter on Dec 10, 2009 2:19 AM
  • 5. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    hello,

    Is the string pool is part of a heap?
    jvm what will do when he encounter the following code,
    String s1 = new String ("hello");
  • 6. Re: object creation of string
    800308 Newbie
    Currently Being Moderated
    punter wrote:
    hsc71 wrote:
    Autoboxing?
    Is this a question or an answer ?

    As per the OP query creating a string object using new creates a new String object ,whereas one without new String corresponds to the String literal being picked from String literal pool
    Correct.
    if existing or creating a new String and adding it to the pool.
    Incorrect... a string literal is a compile time constant, it'll allways be added to literal pool.
    Such strings are not equal by the == operator,but are equals if checked using equals() method.
    To put that in Enlgish: You don't compare String's for equality using the == operator, instead you use [String's equals method|http://java.sun.com/javase/6/docs/api/java/lang/String.html#equals%28java.lang.Object%29] (or equalsIgnoreCase where required).

    >
    THanks.
    HMmm... YOur WElcome.... WTf?

    Cheers. Keith.
  • 7. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    RGEO wrote:
    Is the string pool is part of a heap?
    [http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD]
    As i understand if i write String s = "hello"; the jvm will create a string literal "hello" in a string pool and s is referring to that literal.
    The String literal "hello" will exist in the compiled class file and is loaded to the String pool when the class is loaded.
    and if i write String s1 = new String ("hello"); the jvm will create a new string object in heap and s1 is referring to that object
    The new string object is created on the heap, it shares the same backing char array as the "hello" String literal in the String pool.
    where the "hello" literal go?
    In the String pool.
  • 8. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    THanks.
    HMmm... YOur WElcome.... WTf?

    Cheers. Keith.
    Cheers..HMmm.........WTf?
  • 9. Re: object creation of string
    800308 Newbie
    Currently Being Moderated
    RGEO wrote:
    hello,

    Is the string pool is part of a heap?
    Huh? I suppose yes, you could regard the String pool as part of the heap... but (I guess) that interned String objects would be placed directly into the permanent generation, which is not garbage collected... and therefore I don't really think of the permanent generation as part of the heap (except when tuning heap allocations)... but I think I'm talking over your noob head here, yes... and so to the noob-stuff.

    If you get bored you might like to scan (for now) http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#generations ... and come back to it in a couple of years time (when every second word isn't new and baffling to you;-)
    jvm what will do when he encounter the following code,
    String s1 = new String ("hello");
    The VM will:
    1. Create a new String object, and then
    2. copy the characters 'h','e','l','l','o' from the interned String "hello" to the new String,
    3. and then assign a reference to the new String to variable s1.

    Note that the result is subtley different from String s1 = "hello", which just does step 3... it assigns a reference to the existing interned String object "hello" to the variable s1... it does not create and "populate" a new String object.

    Try this just for fun... what is the output of the following program? Why?
    package forums;
    
    public class StringEquals
    {
      public static void main(String[] args) {
        try {
          String a = "Hello";
          String b = "Hello";
          System.out.println("a==b is "+(a==b));
    
          String c = new String("Hello");
          System.out.println("a==c is "+(a==c));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    Now, what's the correct way to evaluate equality of String objects in Java?

    HTH. Cheers. Keith.
  • 10. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    System.out.println("a==b is "+(a==b));
    returns true becouse a and b reffering same location or address.In string pool there is one string literal "hello" and both a and b reffering to that literal.
    But System.out.println("a==c is "+(a==c)); returns false because c is reffering diffrent memory location. becouse we use new operator to create string object. it will not reffer string pool while making new object.

    correct me if i am wrong.
  • 11. Re: object creation of string
    800308 Newbie
    Currently Being Moderated
    RGEO wrote:
    System.out.println("a==b is "+(a==b));
    returns true becouse a and b reffering same location or address.In string pool there is one string literal "hello" and both a and b reffering to that literal.
    But System.out.println("a==c is "+(a==c)); returns false because c is reffering diffrent memory location. becouse we use new operator to create string object. it will not reffer string pool while making new object.

    correct me if i am wrong.
    You're spot on... a==c returns false because they are distinct objects (which just happen to have the same "value", i.e. they contain the same sequence of characters), and of course the == operator compares references (addresses), NOT values.

    ... some folks (including myself) wander if overloading the + operator to make strings appear to behave a bit like primitives might have been "a bit of a mistake"... it certainly muddies the water for noobs, especially those familiar with javascript (and other "scripting" languages), basic, or indeed C++.... which all "redefine" all (or most of) the arithmetic and logical operators to allow objects to be manipulated with a syntax which is indistinguishable from primitives... Java doesn't do any of that except for ("String"+"String").equals(StringString)... Doh!

    Cheers. Keith.

    Edited by: corlettk on 10/12/2009 21:57 ~~ I hate this new forum markup with a pashion!
  • 12. Re: object creation of string
    843789 Newbie
    Currently Being Moderated
    hsc71 wrote:
    Autoboxing?
    No.
  • 13. Re: object creation of string
    3004 Newbie
    Currently Being Moderated
    punter wrote:
    As per the OP query creating a string object using new creates a new String object ,whereas one without new String corresponds to the String literal being picked from String literal pool if existing or creating a new String and adding it to the pool.
    The String will already exist in the pool by the time that line is executed.
  • 14. Re: object creation of string
    3004 Newbie
    Currently Being Moderated
    RGEO wrote:
    Thanks for your reply,


    Is the string pool is part of a heap?
    It's in the method area which, according to the JVM spec, is "logically part of the heap."
    As i understand if i write String s = "hello"; the jvm will create a string literal "hello" in a string pool and s is referring to that literal.
    Yes.
    and if i write String s1 = new String ("hello"); the jvm will create a new string object in heap and s1 is referring to that object,
    Yes.
    where the "hello" literal go?
    It's still in the constant pool, same as before, but this time we create a new String object copied from it and stored in the "main" part of the heap, and that new String object is what we have a reference to. Since your code still contains the literal "hello", that String is still in the constant pool.
1 2 Previous Next