Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How == and equals() affects Object equivalence in Java ?

Program to demonstrate how Object Equivalence works in Java

class MyInteger{
 
  public int i;
 
  MyInteger(int i){
    this.i =i;
  }
 
}

public class ObjectEquivalenceTest {
 
  public static void main(String[] args) {
  
  Integer one = new Integer(10);
  Integer two = new Integer(10);
  
  System.out.println("one == two : "+ (one == two));
  System.out.println("one != two : "+ (one != two));
  System.out.println("one.equals(two) : "+ (one.equals(two)));
  
  System.out.println();
  
  MyInteger mone = new MyInteger(10);
  MyInteger mtwo = new MyInteger(10);
        
  System.out.println("mone == mtwo : "+ (mone == mtwo));
  System.out.println("mone != mtwo : "+ (mone != mtwo));
  System.out.println("mone.equals(mtwo) : "+ (mone.equals(mtwo))); 

  }

}


Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot