public class ObjectEquals {
public static void main(String [] args){
Person p1 = new Person(20);
Person p2 = new Person(20);
Person p3 = p1;
Integer i1 = new Integer(11);
Integer i2 = new Integer(11);
//实际上下面是上面的缩写!!!
Integer i3 = 25;
Integer i4 = 25;
//注意以下三行,当自动封装包用 ==比较时,比较的时候是Int比较,不能在前后加字符串,只有第三、四行是对的
System.out.println("Integer i3 = new Integer(14) i3 == i4 :" + (i3 == i4) + " **false*把对比的整数放在字符串中间*因为不是指向同一个地址空间**");
System.out.println("************ :" + i3 == i4 + " **false**因为不是指向同一个地址空间**");
System.out.println(i3 == i4);
System.out.println("i3.equals(i4) :" + i1.equals(i4) + " **true**比较内容**");
Integer x = 129;//jdk1.5以后,自动装箱,如果装箱的是一个字节,那么该数据会被共享不会重新开辟空间。
Integer y = 129;
System.out.println(x==y);//
System.out.println(x.equals(y));//true
Cat c1 = new Cat(20);
Cat c2 = new Cat(20);
Cat c3 = new Cat(20);
String a = "name" ;
// int b = 1;
for(int i = 0; i < 10; i++){
a += i;
}
System.out.println("a: " + a);
System.out.println("Integer i1 = new Integer(11) i1 == i2 :" + (i1 == i2) + " **false**因为不是指向同一个地址空间**");
System.out.println("i1.equals(i2) :" + i1.equals(i2) + " **true**比较内容**");
System.out.println("p1 == p2 :" + (p1 == p2) + " **false**因为不是指向同一个地址空间**");
System.out.println("p1.equals(p2) :" + p1.equals(p2) + " **false**非基本数据类型对象包装类,先比较空间再比较内容**");
System.out.println("p1 == p3 :" + (p1 == p3) + " **true**因为指向同一个地址空间**");
System.out.println("p1.equals(p3) :" + p1.equals(p3) + " **true**因为指向同一个地址空间**");
System.out.println("p1.eq(p2) :" + p1.eq(p2) + " **true**重写了比较对象里边的值的方法***");
System.out.println("p1.equals(c1) :" + p1.equals(c1));
//类型错误
// System.out.println("p1.equals(c1) :" + p1.eq(c1));
System.out.println("p1.hashCode() :" + p1.hashCode());
System.out.println("p2.hashCode() :" + p2.hashCode());
System.out.println("p3.hashCode() :" + p3.hashCode());
System.out.println("Integer.toHexString(p1.hashCode()) :" + Integer.toHexString(p1.hashCode()));
System.out.println("Integer.toHexString(p2.hashCode()) :" + Integer.toHexString(p1.hashCode()));
System.out.println("Integer.toHexString(p3.hashCode()) :" + Integer.toHexString(p1.hashCode()));
System.out.println("11 :" +
Integer.toHexString(p1.hashCode()) == Integer.toHexString(p2.hashCode()));
System.out.println("Integer.toHexString(p1.hashCode()) == Integer.toHexString(p3.hashCode()) :"
+ Integer.toHexString(p1.hashCode()) == Integer.toHexString(p3.hashCode()));
System.out.println("p1.getAge() == c1.getAge():" + ( p1.getAge() == c1.getAge() ));
System.out.println("p1.toString() :" +p1.toString());
System.out.println("p2.toString() :" +p2.toString());
System.out.println("p3.toString() :" +p3.toString());
System.out.println("p1.getClass().getName() $ Integer.toHexString(p1.hashCode()) :" +
p1.getClass().getName()+"$"+Integer.toHexString(p1.hashCode()));
}
}
class Person{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(int age) {
this.age = age;
}
public boolean eq(Object obj){
if(! (obj instanceof Person) ){
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return this.age == p.age;
}
}
class Cat{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat(int age) {
this.age = age;
}
}