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

Discussion in 'Assignments and Operators' started by jaya, Dec 23, 2008.

  1. jaya

    jaya New Member

    public class MyClass {

    public Integer startingI;
    public void methodA() {
    Integer i = new Integer(25);
    startingI = i;
    private void methodB(Integer i2) {
    i2 = i2.intValue();
    public static void main(String[] arg){
    MyClass m=new MyClass();

    this gives the output of false true.my doubt is both i2 and statringI is holding the same value 25.bt why this program(==) treating it differently?
  2. vbraojava

    vbraojava New Member

    Hi Jaya,

    In Java Strings are represented by Objects. So if you want to compare two strings are equal or not you should use only equals() method. That method will compare the value hold by those objects. In this case you will get true value.

    If you use == operator to compare, that will not compare the values hold by those objects, instead of that, and it will compare the addresses (hash codes) of two objects. So you will get false because, JVM will not allot same Hash code for two different objects, so definitely you will get false while using == operator.
  3. jaya

    jaya New Member

    sir, bt the below program also giving d output "equal equal".i have declared 1 value as primitive(i2) and the other as wrapper(i1).in dis case will it(==) checks only d value the variable holds?
    public class equaltest

    public static void main(String[] args) {
    Integer i1=new Integer(11);
    int i2=11;
    System.out.println("not equal");


  4. vbraojava

    vbraojava New Member

    Hi Jaya,

    Can you tell which version of Java you are using in your system ?
  5. venkatesh.koduri

    venkatesh.koduri New Member

    There is no difference between
    == Object class .equals();

    but some class are implement Comparable interfaces
    these interface implemented class are must implement .equals()

    Differences between == and equals();

    == :- is applicable for both primitive & objects

    equals():- is applicable for only reference.

    ==:- we can't override this operator, in case of object reference both objects are points same object then true else false if content is same.

    equals():- we can override this method for content comparison(eg String)

    ==:- we con't apply "==" for hetrogenious object voilation C.E saying InComparble Type

    equals():- in this we got false;
    ==:- for any reffernce r; r==null returns false;
    equals():- for any object reference r.equals(null) returns false;
  6. hemanthjava

    hemanthjava New Member

    In java ...when u create an object the JVM assigns a unique number called HASH code.when u use == operator it compares the hash code of those objects but it doesnot compares the content that hold by the object ....

    thank you
  7. hash33

    hash33 New Member

    the operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.

    The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object.

    Example :

    Integer k = new Integer(9);
    Integer l = k;

    in the above code. k == l is true because both k and l refer to the same object.
  8. makr1407

    makr1407 New Member

    public class L
    int i;
    public int hashcode()
    return Integer.toString(i).hashCode();
    public boolean equals(Object obj)
    return i==((L)obj).i;

    public static void main(String[] rst)

    L l1 = new L();
    L l2 = new L();

    System.out.println("equals istrue");
    System.out.println("== is true");


    output is "equals is true" .
    i think it will also give clear idea about equals and ==.
    Last edited: Jul 9, 2012
  9. anneroberts

    anneroberts Member

    By and large both equals() What's more “==” driver On java would used to think about Questions with weigh correspondence At here would some of the contrasts between the two: fundamental distinction between. Equals() strategy and == driver is that person is system What's more other is driver.Both s1 Also s2 alludes on different Questions.

Share This Page