java 代码
- public class HashCodeTest {
- /**
- * @throws java.lang.Exception
- */
- @Before
- public void setUp() throws Exception {}
- /**
- * @throws java.lang.Exception
- */
- @After
- public void tearDown() throws Exception {}
- @Test
- public void testHashCode(){
- String a = new String("AA");
- String b = a;
- String c = new String("AA");
- String d = new String("BB");
- String e = "AA";
- String f = "AA";
- System.out.println("String a hashcode is : "+ a.hashCode());
- System.out.println("String b hashcode is : "+ b.hashCode());
- System.out.println("String c hashcode is : "+ c.hashCode());
- System.out.println("String d hashcode is : "+ d.hashCode());
- System.out.println("String e hashcode is : "+ e.hashCode());
- System.out.println("String f hashcode is : "+ f.hashCode());
- System.out.println("a.equals(b) : "+ a.equals(b));
- System.out.println("a==b : "+ (a==b));
- System.out.println("a.equals(c) : "+ a.equals(c));
- System.out.println("a==c : "+ (a==c));
- System.out.println("b.equals(c) : "+ b.equals(c));
- System.out.println("b==c : "+ (b==c));
- System.out.println("a.equals(e) : "+ a.equals(e));
- System.out.println("a==e : "+ (a==e));
- System.out.println("e.equals(f) : "+ e.equals(f));
- System.out.println("e==f : "+ (e==f));
- Person p1 = new Person();
- Person p2 = p1;
- Person p3 = new Person();
- System.out.println("Model p1 hashcode is : "+ p1.hashCode());
- System.out.println("Model p2 hashcode is : "+ p2.hashCode());
- System.out.println("Model p3 hashcode is : "+ p3.hashCode());
- System.out.println("p1.equals(p2) : "+ p1.equals(p2));
- System.out.println("p1==p2 : "+ (p1==p2));
- System.out.println("p1.equals(p3) : "+ p1.equals(p3));
- System.out.println("p1==p3 : "+ (p1==p3));
- System.out.println("p2.equals(p3) : "+ p2.equals(p3));
- System.out.println("p2==p3 : "+ (p2==p3));
- }
- }
打印的结果为:
java 代码
- String a hashcode is : 2080
- String b hashcode is : 2080
- String c hashcode is : 2080
- String d hashcode is : 2112
- String e hashcode is : 2080
- String f hashcode is : 2080
- a.equals(b) : true
- a==b : true
- a.equals(c) : true
- a==c : false
- b.equals(c) : true
- b==c : false
- a.equals(e) : true
- a==e : false
- e.equals(f) : true
- e==f : true
- Model p1 hashcode is : 12934710
- Model p2 hashcode is : 12934710
- Model p3 hashcode is : 28064776
- p1.equals(p2) : true
- p1==p2 : true
- p1.equals(p3) : false
- p1==p3 : false
- p2.equals(p3) : false
- p2==p3 : false
结论:
待续。。。