stupid java programming question

Discussion in 'OT Technology' started by Yoshiemaster, May 1, 2006.

  1. Yoshiemaster

    Yoshiemaster New Member

    Joined:
    Sep 17, 2005
    Messages:
    1,972
    Likes Received:
    0
    Location:
    Raleigh, NC
    explain the differene between "==", and ".equals"....bitches.
     
  2. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,169
    Likes Received:
    500
    == is an operator that's not overloaded for the String class.

    String.equals() is a member function of the String class that does a character by character comparison of the parent String object and the argument to equals().
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,169
    Likes Received:
    500
  4. Yoshiemaster

    Yoshiemaster New Member

    Joined:
    Sep 17, 2005
    Messages:
    1,972
    Likes Received:
    0
    Location:
    Raleigh, NC

    thanks, that was a good answer.
     
  5. bradpitt

    bradpitt New Member

    Joined:
    Feb 22, 2005
    Messages:
    982
    Likes Received:
    0
    Location:
    █ i'm #1, hollywood █
    you cant test if two objects are equal using ==, you would need to use .equals method, this applies to all objects not just strings. If you are writing your own object, you would have to write your own equals method or implement comparable.
     
  6. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    == is also used for base types, like int, double, char, etc.

    .equal is used for everything else
     
  7. Dmar

    Dmar Jump

    Joined:
    May 9, 2004
    Messages:
    344
    Likes Received:
    0
    Some of the above answers aren't actually correct.

    == tests for reference equality. It will return true if the variables it tests refer to the exact same instance of an object (or primitive).

    The equals method tests for whether two instances of an object are equal. What properties of the object that make them equal is left for the user implementing the class to decide.

    For example:

    Point a = new Point(1,2);
    Point b = new Point(1,2);

    a == b will return false.
    a.equals(b) will return true.
     
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,169
    Likes Received:
    500
    In what way is that different from the answers we posted?
     
  9. Dmar

    Dmar Jump

    Joined:
    May 9, 2004
    Messages:
    344
    Likes Received:
    0
    oh your answer is correct. i was just generalizing...it just seems like a lot of people don't actually realize that they often need to override the equals method for any objects you write yourself for it to work appropriately (at least as defined by the Object contract). Even less seem to realize that overriding equals means you are also required to override hashcode.
     

Share This Page