Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equals和==有什么区别?对象的equals判断的是什么? #208

Closed
zhonghuasheng opened this issue Mar 19, 2020 · 0 comments
Closed

Comments

@zhonghuasheng
Copy link
Owner

zhonghuasheng commented Mar 19, 2020

  1. 基础数据类型没有equals方法,只能使用==来判断,判断的时候是判断值是否相等
  2. 引用类型可以使用==和equalls。如果对象不重写Object中的equals方法,那么==和equals没有区别,因为Object中的equals也是使用==来判断;如果重写了equals方法,那就使用对象重写的equals方法来判断。==比较的是两个对象在堆中存放数据的内存地址是否相等。
  3. String需要拿来特殊说明,String是Java中不需要new就可以产生对象的特例。使用String来申明一个变量的时候,JVM会在常量池中查找是否已经存在这个值,如果存在就把内存地址返回给变量;如果不存在,会新开辟一个空间来存储这个值,然后把内存地址返回。那么我们再来看String中==和equals的区别,String中使用==比较的是内存地址是否相等,String重写了Object中的equals方法,使用equals时先比较对象是否相等,如果相等就返回true;否则比较值是否相等。
    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;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant