[Java学习笔记] 对象的比较(引用比较、值比较) ^_^个人的理解,请多多关照^_^

1、对于基本数据类型,“==”比较的是值。基本数据类型没有“equals”方法。

基本的数据类型有boolean,char,byte,short,int,long,float,double,void。注意String不是基本数据类型。

int i1 = 10;
int i2 = 10;
System.out.println("i1's identityHashCode=" + System.identityHashCode(i1)); 
System.out.println("i2's identityHashCode=" + System.identityHashCode(i2));
System.out.println("[i1 ==  i2] -> " + (i1 == i2) + "\n");

i1 = 10;
i2 = Integer.parseInt("10");
System.out.println("i1's identityHashCode=" + System.identityHashCode(i1)); 
System.out.println("i2's identityHashCode=" + System.identityHashCode(i2));
System.out.println("[i1 ==  i2] -> " + (i1 == i2) + "\n");
运行结果:

i1's identityHashCode=4072869
i2's identityHashCode=4072869
[i1 ==  i2] -> true

i1's identityHashCode=4072869
i2's identityHashCode=4072869
[i1 ==  i2] -> true


2、对于“非基本数据类型”,“==”比较的是对象的引用值,“equals”比较的也是引用值。可以通过重写equals方法实现值的比较。

class Number {
	public Number(int i){
		this.i = i;
	}
	int i;
}

public class Assignment {
	public static void main(String[] args) {
		Number n1 = new Number(10);
		Number n2 = new Number(10);
		System.out.println("n1's hashcode=" + n1.hashCode());
		System.out.println("n2's hashcode=" + n2.hashCode());
		System.out.println("n1's identityHashCode=" + System.identityHashCode(n1));
		System.out.println("n2's identityHashCode=" + System.identityHashCode(n2));
		System.out.println("[n1 == n2] -> " + (n1 == n2));
		System.out.println("[n1.equals(n2)] -> " + (n1.equals(n2)) + "\n");
	}
}

运行结果:

n1's hashcode=1671711n2's hashcode=11394033
n1's identityHashCode=1671711
n2's identityHashCode=11394033
[n1 == n2] -> false
[n1.equals(n2)] -> false

此时如果要比较对象的具体内容,而不是比较对象的引用,那么就要重写equals方法。

class Number {
	public Number(int i){
		this.i = i;
	}
	int i;
	
	public boolean equals(Object obj){
	if(obj != null && obj instanceof Number){
		if(this.i == ((Number)obj).i){
			return true;
		}
	}
	return false;
}
}

public class Assignment {
	public static void main(String[] args) {
		Number n1 = new Number(10);
		Number n2 = new Number(10);
		System.out.println("n1's hashcode=" + n1.hashCode());
		System.out.println("n2's hashcode=" + n2.hashCode());
		System.out.println("n1's identityHashCode=" + System.identityHashCode(n1));
		System.out.println("n2's identityHashCode=" + System.identityHashCode(n2));
		System.out.println("[n1 == n2] -> " + (n1 == n2));
		System.out.println("[n1.equals(n2)] -> " + (n1.equals(n2)) + "\n");
	}
}
运行结果:

n1's hashcode=1671711
n2's hashcode=11394033
n1's identityHashCode=1671711
n2's identityHashCode=11394033
[n1 == n2] -> false
[n1.equals(n2)] -> true


3、String不属于基本数据类型。但是“==”和“equals”的比较与上面的对象比较不一样。

    “==”比较的是对象的”引用“,而equals比较的是“值“。

String s1 = "abc";
String s2 = "abc";
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2)  + "\n");
运行结果: 此时,内存中只保留一份”abc“的空间。这个与常量的存储方式有关。因此s1与s2的hashcode一样,值也一样。

s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=4072869
s2's identityHashCode=4072869
[s1 == s2] -> true
[s1.equals(s2)] -> true

s1 = new String("abc");
s2 = new String("abc");
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2)  + "\n");
运行结果:此时,s1和s2分别指向两个不同的存储单元。因此hashcode不一样,但是值是一样的。
s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=1671711
s2's identityHashCode=11394033
[s1 == s2] -> false
[s1.equals(s2)] -> true
String s1 = "abc";
String s2 = "abcd".substring(0,3);
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2));
运行结果:这种情况下,也是分别指向两个不同的存储单元。

s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=4072869
s2's identityHashCode=1671711
[s1 == s2] -> false
[s1.equals(s2)] -> true

在这里我们也可以看到,”引用“应该是指System.identityHashCode(obj),而不是obj.hashCode();

所以我个人认为String的比较用equals比较。 如果比较是否是同一个引用,那么就用”==“。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值