What is the difference between equals() and == ?

Discussion in 'Object Oriented Concepts' started by sreenu3112, May 29, 2010.

  1. sreenu3112

    sreenu3112 New Member

    according to java.lang.Object class equals() defined as

    public boolean equals(Object object) {
    return this == object;
    }

    Ex:
    class EqualsNotEqualTo {
    public static void main(String args[]) {
    String s1 = "Hello";
    String s2 = new String(s1);
    System.out.println(s1 + " equals " + s2 + " -> " +
    s1.equals(s2));
    System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
    }
    }

    output:
    Hello equals Hello -> true
    Hello == Hello -> false
    how it will differ, (bcoz equlas() internally compares using == operator ) ????????????????????????

    Can anybody plz reply .................
     
  2. sandeepkumar05497

    sandeepkumar05497 New Member

    Re: wt is the difference between equals() and == ?

    the equlas(Object o) present in Object class is ment for reference comparasion so the functionality of equals(Object o) present in Object class and == is same.The equals(Object o) present in String class is for content comparasion. the equals(Object o) present inString class is overridden for content comparasion.


    hope so u understand this concept

    Thanks&Regards
    Sandeep Kumar M
     
  3. nagaraju48458

    nagaraju48458 New Member

    Re: wt is the difference between equals() and == ?

    here
    == operator compare the reference of the object
    equals() method compare the contents of the object
     
  4. anneroberts

    anneroberts Member

    The point when utilized with objects, those == driver compares the two item references Furthermore determines if they allude of the same example. Equals() is a strategy accessible in the string population that is used to look at two strings What's more figure out if they are rise to.
     

Share This Page