Two identical Strings stored in two different object

Discussion in 'Java' started by Neroku, Feb 12, 2007.

  1. Neroku

    Neroku Guest

    Hi, I know java ensures that only one String object exists when there
    are two identicals Strings, i.e:

    String s1 = "Hello";
    String s2 = "Hello";
    System.out.println(s1==s2);

    It prints "true"

    However:

    String s1 = "Hello";
    String s2 = new String("Hello");
    System.out.println(s1==s2);

    prints "false"

    I don't understand why, since both Strings hold the same data. Any
    ideas?

    TIA
     
    Neroku, Feb 12, 2007
    #1
    1. Advertising

  2. Neroku wrote:
    > Hi, I know java ensures that only one String object exists when there
    > are two identicals Strings, i.e:
    >
    > String s1 = "Hello";
    > String s2 = "Hello";
    > System.out.println(s1==s2);
    >
    > It prints "true"
    >
    > However:
    >
    > String s1 = "Hello";
    > String s2 = new String("Hello");
    > System.out.println(s1==s2);
    >
    > prints "false"
    >
    > I don't understand why, since both Strings hold the same data. Any
    > ideas?


    "Hello" and "Hello" becomes one object.

    new String("Hello") means create a new object and
    allocate memory for that and copy the input string
    to it.

    I am not sure that Java require it to be another object,
    but from a practical implementation point of view
    I consider it very natural that it is.

    Try consider writing the Java code for that constructor.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 12, 2007
    #2
    1. Advertising

  3. Neroku

    Lew Guest

    Neroku wrote:
    >> Hi, I know java ensures that only one String object exists when there
    >> are two identicals Strings, i.e:


    Only for literals and one other case, mentioned below.

    >> String s1 = "Hello";
    >> String s2 = "Hello";
    >> System.out.println(s1==s2);
    >>
    >> It prints "true"
    >>
    >> However:
    >>
    >> String s1 = "Hello";
    >> String s2 = new String("Hello");
    >> System.out.println(s1==s2);
    >>
    >> prints "false"
    >>
    >> I don't understand why, since both Strings hold the same data. Any
    >> ideas?


    Arne Vajhøj wrote:
    > "Hello" and "Hello" becomes one object.


    Because both are literals.

    > new String("Hello") means create a new object and
    > allocate memory for that and copy the input string
    > to it.


    Because it isn't a literal.

    > I am not sure that Java require it to be another object,
    > but from a practical implementation point of view
    > I consider it very natural that it is.
    >
    > Try consider writing the Java code for that constructor.


    Read about the String.intern() method:
    <http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern()>

    - Lew
     
    Lew, Feb 12, 2007
    #3
  4. Neroku

    Karl Uppiano Guest

    "Lew" <> wrote in message
    news:...
    > Neroku wrote:
    >>> Hi, I know java ensures that only one String object exists when there
    >>> are two identicals Strings, i.e:

    >
    > Only for literals and one other case, mentioned below.
    >
    >>> String s1 = "Hello";
    >>> String s2 = "Hello";
    >>> System.out.println(s1==s2);
    >>>
    >>> It prints "true"
    >>>
    >>> However:
    >>>
    >>> String s1 = "Hello";
    >>> String s2 = new String("Hello");
    >>> System.out.println(s1==s2);
    >>>
    >>> prints "false"
    >>>
    >>> I don't understand why, since both Strings hold the same data. Any
    >>> ideas?

    >
    > Arne Vajhøj wrote:
    >> "Hello" and "Hello" becomes one object.

    >
    > Because both are literals.
    >
    >> new String("Hello") means create a new object and
    >> allocate memory for that and copy the input string
    >> to it.

    >
    > Because it isn't a literal.
    >
    >> I am not sure that Java require it to be another object,
    >> but from a practical implementation point of view
    >> I consider it very natural that it is.
    >>
    >> Try consider writing the Java code for that constructor.

    >
    > Read about the String.intern() method:
    > <http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern()>
    >
    > - Lew


    I think they use the same character buffer though?
     
    Karl Uppiano, Feb 12, 2007
    #4
  5. Neroku

    Corona4456 Guest

    On Feb 11, 5:21 pm, "Neroku" <> wrote:
    > Hi, I know java ensures that only one String object exists when there
    > are two identicals Strings, i.e:
    >
    > String s1 = "Hello";
    > String s2 = "Hello";
    > System.out.println(s1==s2);
    >
    > It prints "true"
    >
    > However:
    >
    > String s1 = "Hello";
    > String s2 = new String("Hello");
    > System.out.println(s1==s2);
    >
    > prints "false"
    >
    > I don't understand why, since both Strings hold the same data. Any
    > ideas?
    >
    > TIA


    Java is a reference language so when you call the '==' it compares
    addresses not values. If you want to compare Strings use the equals
    function (i.e. System.out.println(s1.equals(s2)) should print true)
     
    Corona4456, Feb 12, 2007
    #5
  6. Neroku

    bigbighd604 Guest

    Java has a special optimize mechanism for declaring a new String
    Object using the format 'String sx = "xxx"', it will try to find
    whether already has a String Object represents "xxx", if there then
    return that Object's reference or create a new one. But the format
    'new String("xxx")' will always create a new String Object.

    so when you type:
    String s1 = "Hello";
    Stting s2 = new String("Hello");
    There will be two String objects.

    String s1 = new String("Hello");
    String s2 = "Hello";
    You will get the same String Ojbect.

    On 2ÔÂ12ÈÕ, ÉÏÎç8ʱ21·Ö, "Neroku" <> wrote:
    > Hi, I know java ensures that only one String object exists when there
    > are two identicals Strings, i.e:
    >
    > String s1 = "Hello";
    > String s2 = "Hello";
    > System.out.println(s1==s2);
    >
    > It prints "true"
    >
    > However:
    >
    > String s1 = "Hello";
    > String s2 = new String("Hello");
    > System.out.println(s1==s2);
    >
    > prints "false"
    >
    > I don't understand why, since both Strings hold the same data. Any
    > ideas?
    >
    > TIA
     
    bigbighd604, Feb 12, 2007
    #6
  7. Neroku

    Lew Guest

    "Neroku" <> wrote:
    >> Hi, I know java ensures that only one String object exists when there
    >> are two identicals Strings, i.e:
    >>
    >> String s1 = "Hello";
    >> String s2 = "Hello";
    >> System.out.println(s1==s2);
    >>
    >> It prints "true"
    >>
    >> However:
    >>
    >> String s1 = "Hello";
    >> String s2 = new String("Hello");
    >> System.out.println(s1==s2);
    >>
    >> prints "false"
    >>
    >> I don't understand why, since both Strings hold the same data. Any
    >> ideas?
    >>
    >> TIA


    Corona4456 wrote:
    > Java is a reference language so when you call the '==' it compares
    > addresses not values. If you want to compare Strings use the equals
    > function (i.e. System.out.println(s1.equals(s2)) should print true)


    That's not the issue. The OP was discussing the interning of Strings. At issue
    is object equality, not value equality, hence == is what is needed.

    - Lew
     
    Lew, Feb 12, 2007
    #7
  8. Neroku

    Lew Guest

    Please do not top post.

    bigbighd604 wrote:
    > Java has a special optimize mechanism for declaring a new String
    > Object using the format 'String sx = "xxx"', it will try to find
    > whether already has a String Object represents "xxx", if there then
    > return that Object's reference or create a new one. But the format
    > 'new String("xxx")' will always create a new String Object.


    Correct. Where "it" looks for these strings is in a private store within the
    String class.

    > so when you type:
    > String s1 = "Hello";
    > Stting s2 = new String("Hello");
    > There will be two String objects.
    >
    > String s1 = new String("Hello");
    > String s2 = "Hello";
    > You will get the same String Ojbect.


    You were right the first time, but wrong the second. Both examples will yield
    distinct String objects for s1 and s2.

    - Lew
     
    Lew, Feb 12, 2007
    #8
  9. Corona4456 wrote:
    > On Feb 11, 5:21 pm, "Neroku" <> wrote:
    >> Hi, I know java ensures that only one String object exists when there
    >> are two identicals Strings, i.e:
    >>
    >> String s1 = "Hello";
    >> String s2 = "Hello";
    >> System.out.println(s1==s2);
    >>
    >> It prints "true"
    >>
    >> However:
    >>
    >> String s1 = "Hello";
    >> String s2 = new String("Hello");
    >> System.out.println(s1==s2);
    >>
    >> prints "false"
    >>
    >> I don't understand why, since both Strings hold the same data. Any
    >> ideas?

    >
    > Java is a reference language so when you call the '==' it compares
    > addresses not values. If you want to compare Strings use the equals
    > function (i.e. System.out.println(s1.equals(s2)) should print true)


    That is not really what the original poster is asking about.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 12, 2007
    #9
  10. bigbighd604 wrote:
    > Java has a special optimize mechanism for declaring a new String
    > Object using the format 'String sx = "xxx"', it will try to find
    > whether already has a String Object represents "xxx", if there then
    > return that Object's reference or create a new one. But the format
    > 'new String("xxx")' will always create a new String Object.
    >
    > so when you type:
    > String s1 = "Hello";
    > Stting s2 = new String("Hello");
    > There will be two String objects.


    Yep.

    > String s1 = new String("Hello");
    > String s2 = "Hello";
    > You will get the same String Ojbect.


    Are you sure ?

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Feb 12, 2007
    #10
  11. "Neroku" <> wrote in message
    news:...
    > Hi, I know java ensures that only one String object exists when there
    > are two identicals Strings


    No, Java doesn't ensure that for all strings. It does have a collections of
    unique Strings, but not all Strings belong to it. Literal Strings do, and
    the return value from String.intern() does.

    > , i.e:
    >
    > String s1 = "Hello";
    > String s2 = "Hello";
    > System.out.println(s1==s2);
    >
    > It prints "true"


    Yes, these are literal Strings.

    >
    > However:
    >
    > String s1 = "Hello";
    > String s2 = new String("Hello");
    > System.out.println(s1==s2);
    >
    > prints "false"


    s2 is not a literal String. Note that "new" is guaranteed to create a
    brand-new object, so it would be quite incorrect for this to be true.

    But

    String s3 = s2.intern();
    System.out.println(s1==s3)

    will once again be "true".
     
    Mike Schilling, Feb 12, 2007
    #11
  12. Neroku

    Oliver Wong Guest

    "Karl Uppiano" <> wrote in message
    news:GfPzh.1775$TG6.1713@trnddc06...
    >

    [on the topic of "Hello" and new String("Hello")]
    >
    > I think they use the same character buffer though?


    Perhaps they do, but the == operator doesn't compare character buffers.
    It compares object references, and the two references refer to different
    objects.

    - Oliver
     
    Oliver Wong, Feb 12, 2007
    #12
  13. Neroku

    Oliver Wong Guest

    "Arne Vajhøj" <> wrote in message
    news:45cfd759$0$49202$...
    > bigbighd604 wrote:
    >> Java has a special optimize mechanism for declaring a new String
    >> Object using the format 'String sx = "xxx"', it will try to find
    >> whether already has a String Object represents "xxx", if there then
    >> return that Object's reference or create a new one. But the format
    >> 'new String("xxx")' will always create a new String Object.

    [...]
    >> String s1 = new String("Hello");
    >> String s2 = "Hello";
    >> You will get the same String Ojbect.

    >
    > Are you sure ?


    <SSCCE>
    public class Test {
    public static void main(String[] args) {
    String s1 = new String("Hello");
    String s2 = "Hello";
    System.out.println(s1 == s2);
    }
    }
    </SSCCE>

    <output>
    false
    </output>

    The "special optimization mechanism" happens at compile time. The
    compiler adds a "constant pool" to every .class file it generates, storing
    any constants (string literals, among other things) that class file needs,
    and only stores one copy for constants that appear repeatedly in the source.
    Allocating the string, via the "new" operator, happens at runtime.

    - Oliver
     
    Oliver Wong, Feb 12, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    3
    Views:
    495
  2. GG
    Replies:
    1
    Views:
    427
    Roedy Green
    Jul 14, 2004
  3. Prabh
    Replies:
    4
    Views:
    4,147
    Malcolm Dew-Jones
    Aug 11, 2004
  4. ann
    Replies:
    13
    Views:
    672
    Patricia Shanahan
    Sep 13, 2005
  5. chris Struhar
    Replies:
    1
    Views:
    311
    Jacques Labuschagne
    Jul 19, 2003
Loading...

Share This Page