Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot