Created
December 28, 2015 08:14
-
-
Save not-for-me/b12621372b54453973a1 to your computer and use it in GitHub Desktop.
In Java, 'equals' and '==' test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EqualTest{ | |
public static void main(String []args){ | |
new EqualTest().test(); | |
} | |
private void test() { | |
Dummy obj1 = new Dummy(1, 10.0); | |
Dummy obj2 = new Dummy(2, 20.0); | |
Dummy obj3 = new Dummy(1, 10.0); | |
System.out.println("obj1 == obj2: " + (obj1 == obj2) + " / obj1.equals(obj2): " + obj1.equals(obj2) ); | |
System.out.println("obj1 == obj3: " + (obj1 == obj3) + " / obj1.equals(obj3): " + obj1.equals(obj3) ); | |
} | |
class Dummy { | |
private int data1; | |
private double data2; | |
public Dummy() { | |
} | |
public Dummy(int data1, double data2) { | |
this.data1 = data1; | |
this.data2 = data2; | |
} | |
public void setData1(int data1) { | |
this.data1 = data1; | |
} | |
public void setData2(double data2) { | |
this.data2 = data2; | |
} | |
public int getData1() { | |
return data1; | |
} | |
public double getData2() { | |
return data2; | |
} | |
public boolean equals(Dummy that) { | |
return this.data1 == that.data1 && this.data2 == that.data2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment