Program to demonstrate how Object Equivalence works in Java
Output of the program :
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 :