equals vs ==

Discussion in 'Java' started by Guest, Dec 18, 2004.

  1. Guest

    Guest Guest

    if (s instanceof String) {
    if (s == "a string");
    if (s.equals("a string"));
    }

    what is the difference between s.equals and s == ???

    When I use s == "something", many times if s == "something", returns false!

    Instead s.equals works correct always.

    Is there something that I dont know?
    Guest, Dec 18, 2004
    #1
    1. Advertising

  2. "<- Chameleon ->" <> scribbled the following:
    > if (s instanceof String) {
    > if (s == "a string");
    > if (s.equals("a string"));
    > }


    > what is the difference between s.equals and s == ???


    > When I use s == "something", many times if s == "something", returns false!


    > Instead s.equals works correct always.


    > Is there something that I dont know?


    == compares identity, equals() compares equality. Two object references
    will only ever return true from == if they refer to the *EXACT* *SAME*
    object. "Exact same" meaning there being only one object, with two
    references referring to it. OTOH, equals() will return also true if the
    references refer to two objects that are considered equal. The
    consideration depends on the object class. It defaults to being the
    same as ==. String overrides it by comparing the contents of the string.
    Other objects can override it in other ways.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "We sorcerers don't like to eat our words, so to say."
    - Sparrowhawk
    Joona I Palaste, Dec 18, 2004
    #2
    1. Advertising

  3. Guest

    Phil Staite Guest

    s.equals(x);

    Will return true if the strings s and x contain the same character
    sequence. So:

    String s = new String("foobar");
    String x = new String("foobar");

    Then s.equals(x) will return true, but s == x will be false, because s
    and x are not the same String Object.

    If you then do:

    String y = s;
    String t = x;

    You have a case where any string of the 4, .equals() any other string of
    the 4 (s, t, x, y) will return true, but only s == y or t == x will
    return true. That is because they refer to the same object.
    Phil Staite, Dec 18, 2004
    #3
  4. You may want to read this -
    http://www.geocities.com/technofundo/tech/java/equalhash.html

    - Manish


    "<- Chameleon ->" <> wrote in message
    news:cq283t$rom$...
    > if (s instanceof String) {
    > if (s == "a string");
    > if (s.equals("a string"));
    > }
    >
    > what is the difference between s.equals and s == ???
    >
    > When I use s == "something", many times if s == "something", returns

    false!
    >
    > Instead s.equals works correct always.
    >
    > Is there something that I dont know?
    Manish Hatwalne, Dec 19, 2004
    #4
  5. Guest

    john martin Guest

    <- Chameleon -> wrote:
    > if (s instanceof String) {
    > if (s == "a string");
    > if (s.equals("a string"));
    > }
    >
    > what is the difference between s.equals and s == ???
    >
    > When I use s == "something", many times if s == "something", returns false!
    >
    > Instead s.equals works correct always.
    >
    > Is there something that I dont know?


    If you've ever worked with C or C++, it might actually be easier to
    think of this in terms of pointers. Though Java doesn't have pointer
    arithmetic, you can think of an object, such as the String s, as a
    variable name (in this case s) pointing to the actual object. So, if
    you compare two Strings as follows:

    String s1 = new String("foo");
    String s2 = new String("foo");

    if(s1==s2)
    System.out.println("equal");
    else
    System.out.println("not equal");

    "not equal" will be the output, because s1 and s2 are not equal: they
    point to different String objects.


    If, however, you change it to:

    String s1 = new String("foo");
    String s2 = new String("foo");

    if(s1.equals(s2))
    System.out.println("equal");
    else
    System.out.println("not equal");

    you'll get the expected output ("equal"), because the equals method is
    overloaded to do a lexical comparison on the two String objects (in the
    case of the String class, other objects can have their equals method
    overloaded to do the appropriate comparison).

    Often you'll want to overload the equals method for classes of your own
    when you want to compare them to determine whether two objects should be
    considered to have equal value regardless of whether they are in fact
    pointing to the same instance. E.g., if you have a class with two
    fields, and instances of that class can be considered equal when the
    first field in the first object is the same as the first field in the
    second object, and the second field in the first object is the same as
    the second field in the second object, you'd override the equals method
    to check for that condition and then return true if and only if that is
    the case.

    Hope that helps. I know it could be a somewhat confusing explanation
    since it's often said that Java doesn't use pointers (and it's true that
    it doesn't do pointer arithmetic at all). But it is often useful to
    think of an object variable as a pointer to the object in memory (you
    even have to use "new" to allocate the object; you can't just declare an
    object on the stack the way you can a primitive such as int), even if
    only for semantic purposes when thinking about the way the program will
    work.
    john martin, Dec 19, 2004
    #5
  6. Guest

    Ryan Stewart Guest

    "john martin" <> wrote in message
    news:cq3n6g$qpg$...
    > Hope that helps. I know it could be a somewhat confusing explanation
    > since it's often said that Java doesn't use pointers


    Who often says that?
    Ryan Stewart, Dec 19, 2004
    #6
  7. <- Chameleon -> wrote:
    > if (s instanceof String) {
    > if (s == "a string");
    > if (s.equals("a string"));
    > }
    >
    > what is the difference between s.equals and s == ???
    >
    > When I use s == "something", many times if s == "something", returns false!
    >
    > Instead s.equals works correct always.
    >
    > Is there something that I dont know?



    "equals" physically goes and compares the contents of s and "something"
    character by character.

    "==" simply tests if the contents of s and "something" reside at the
    same place in memory.... :-> See... and this is the tricky part... Part
    of the way Java manages memory, objects, and strings in particular is to
    try to have only one copy of a unique string in memory at a time. So if
    you happen to mention the string "something" 15 times in your program,
    Java will attempt to store it in one place in memory and just reference
    that.

    The main exception is when you do a new String.

    --min
    Lewis Sellers, Dec 19, 2004
    #7
  8. Guest

    john martin Guest

    Ryan Stewart wrote:
    > "john martin" <> wrote in message
    > news:cq3n6g$qpg$...
    >
    >>Hope that helps. I know it could be a somewhat confusing explanation
    >>since it's often said that Java doesn't use pointers

    >
    >
    > Who often says that?
    >
    >


    i've heard a number of people say that, although i don't think i've ever
    seen anything i'd consider a "good" text on java say it (and i don't
    think i've ever heard anybody reasonably knowledgable of java say it).
    i've been asked in job interviews whether java uses pointers and it's
    always apparent that "yes" is not the correct answer without some
    explanation (e.g., "yes, but there's no pointer pointer arithmetic or
    dereferencing" etc).
    john martin, Dec 20, 2004
    #8
  9. Guest

    Tony Morris Guest

    > (e.g., "yes, but there's no pointer pointer arithmetic or
    > dereferencing" etc).


    You don't get the job.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
    Tony Morris, Dec 20, 2004
    #9
  10. In article <9ksxd.80652$>,
    "Tony Morris" <> wrote:

    > > (e.g., "yes, but there's no pointer pointer arithmetic or
    > > dereferencing" etc).

    >
    > You don't get the job.


    The one time I was asked that, I asked whether the interviewer meant
    from a programmer's perspective, from a JVM perspective, or from a JIT
    designer's perspective. (Not as off the wall a distinction as it sounds
    - the company did a lot of JNI, and providing a better jitc was not
    completely out of scope.) Apparently, there were plus points for asking
    whether he considered a C++ reference as a pointer or not.

    I got that job.

    Scott
    Scott Ellsworth, Dec 20, 2004
    #10
    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. Andreas Klemt
    Replies:
    3
    Views:
    1,761
    Natty Gur
    Aug 10, 2003
  2. fake ID
    Replies:
    1
    Views:
    14,407
  3. fake ID
    Replies:
    0
    Views:
    587
    fake ID
    Feb 10, 2006
  4. Tom
    Replies:
    12
    Views:
    8,052
    Chris Smith
    Aug 5, 2003
  5. Edward A Thompson
    Replies:
    4
    Views:
    525
    Tony Morris
    Feb 11, 2004
Loading...

Share This Page