This discussion is archived
2 Replies Latest reply: Aug 27, 2009 10:48 AM by 796262 RSS

String object equivalency

843789 Newbie
Currently Being Moderated
Maybe someone could explain this to me.

String a="abc";
String b="abc";

a == b // true

I thought == compared the object reference, not the value. Is == overloaded in the String object or are Strings created differently than other objects so the two different variables really are pointing at the same object?
  • 1. Re: String object equivalency
    796447 Newbie
    Currently Being Moderated
    jamesss wrote:
    Maybe someone could explain this to me.

    String a="abc";
    String b="abc";

    a == b // true

    I thought == compared the object reference, not the value.
    Yep.
    Is == overloaded in the String object
    No
    or are Strings created differently than other objects so the two different variables really are pointing at the same object?
    They're not created differently than other objects other than that here you didn't need to use the "new" keyword. They are referencing the same literal constant in the string pool.
  • 2. Re: String object equivalency
    796262 Newbie
    Currently Being Moderated
    [This article|http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html] has a pretty good newbie-safe explanation of what's happening, as well.