java中==和equals的比较

一、含义

在java中,==和equals都是比较是否相等的。

==:比较的是地址,也就是比较内存中所存储的数值是否相等。所以比较基本类型的数据或者两个引用变量(地址)是否相等。

equals:比较的是内容。比较的不是引用,而是因为里面存储的内容。比如,对象的比较,比较的不是对象的地址,而是对象里面的属性。

二、详解

		int a = 1;
		int b = 1;
		System.out.println(a == b);
上面就是比较基本类型的,输出是正确,这是很简单的。但是如果是比较String呢?

		String str1 = new String("hello");
		String str2 = new String("hello");
		System.out.println(str1 == str2);

输出就false。因为new创建的是两个对象的地址,这样str1和str2肯定是两个不同的对象地址,比较肯定是不一样的。

如果是下面这句呢?

System.out.println(str1.equals(str2));
那输出就是true。

这里展现的就是两者比较东西的不同。那接下来我们看看源码。

三、源码

equals是Object的一个比较方法,打开源码如下:

    public boolean equals(Object obj) {
        return (this == obj);
    }
那这样不就是和==一样吗?是的,如果是直接调用Object里面的equals,那的确是用==比较,比较的也是内存里面的地址,那这样str1和str2应该是一样的。但是我们上面测试过了。结果是不一样,原因就是String类中对Object里面的equals方法做了重写。打开(快捷键F3)String类可以看到里面的equals方法如下:

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }
这个方法就是比较的是内容了。对源码有兴趣的自己看,不难。

所以为什么我们比较对象的时候,明明两个对象是不一样的地址,只不过一样的内容,用==就是false,用equsls就是正确的。

四、扩展
看看这个下面这个代码
		String c = "hello";
		String d = "hello";
		String str1 = new String("hello");
		String str2 = new String("hello");
		System.out.println("c == d" + (c == d));
		System.out.println("c.equals(str1)" + c.equals(str1));
		System.out.println("str1 == str2" + (str1 == str2));
		System.out.println("str1.equals(str2)" + str1.equals(str2));
结果如下:
c == d  true
c.equals(str1) true
str1 == str2 false
str1.equals(str2) true
按照上面说c和d是一个对象,那为什么回事true,其实这里不是这样的。这里并没有去new一个实例化对象出来。String c = “hello”;是在堆内存中新建一个char[]的字符数组,然后c作为一个引用变量指向这个数组地址。而d并不是直接新建一个hello,而是检查内存中是否存在hello,如果存在,这d直接指向这个地址,所以c==d才会返回true。

而String str1 = new String("hello");实际上是创建了两个引用。第一个是hello的堆内存地址,另外一个是str1的栈对象里面的地址。这样其实就是新建了两个对象,而str1是指向hello这个地址的。所以,比较str1和str2的时候,如果是比较地址,那肯定是不一样的,如果是比较内容,那就是一样的。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值