各种比较,ObjectEquals

本文深入探讨了Java中对象比较的概念及实现方式,包括使用==进行地址比较与equals方法进行内容比较的区别。通过具体实例,解释了自动装箱、基本数据类型包装类及自定义类在不同情况下的比较行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值