java - 关于Object中的equals方法和String中的equals方法的疑惑?
阿神
阿神 2017-04-17 13:52:08
0
4
750

首先是这段代码:

public class test {
    public static void main(String[] args) {
        String s1 = "Monday";
        String s2 = new String("Monday");
        if (s1 == s2){
            System.out.println("s1 == s2");}
        else{
            System.out.println("s1 != s2");
        }
        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        }else{
            System.out.println("s1 not equals s2");
        }
    }
}

输出结果是:s1 != s2 和 s1 equals s2; 这个我可以理解,使用new操作符后,在堆内存中又新开辟了一块空间,s1和s2在堆内存中的值相同,但是引用的地址不同。但是在博客园的这篇文章看到下面这段代码:

public class test{
    public static void main(String[] args)
    {
        test obj1 = new test();
        test obj2 = new test();

        if(obj1 == obj2){
            System.out.println("obj1 == obj2");
        }else{
            System.out.println("obj1 != obj2");
        }
        if(obj1.equals(obj2)){
            System.out.println("obj1 equals obj2");
        }else{
            System.out.println("obj1 not equals obj2");
        }
    }
}

输出: obj1 != obj2 obj1 not equals obj2
昨天晚上看了那篇文章的评论,把我自己的理解写出来,但是还是不是很清楚,我大意是这样的:
java当中所有类都继承自Object这个基类,在Object中的定义了一个equals方法,这个方法的初始化行为是比较对象的内存地址值(Object的equals方法使用==比较的),但在一些类库中这个方法被覆盖掉了,比如String, Interger, Date这些类中equals有其自己的实现方法,String类继承自Object类,也继承了equals方法,但是重写了该方法,不再比较类在堆内存中的存放地址了,而是比较存在堆中的值。 ???
这个解释不知道对不对,望指教,还有,关于obj1 not equals obj2你们是怎么看的??

阿神
阿神

闭关修行中......

reply all(4)
大家讲道理

== Needless to say, comparison is a comparison of memory addresses in any case.
equals comparison is a method call. The default implementation (Object class) uses ==:

public boolean equals(Object obj) {
    return (this == obj);
}

First code:
What is compared is the String object, and the String class overrides the equals() method and what is compared is the string content, so s1 equals s2 is output.

You have no problem understanding this!

Second code:
What is compared is the test object. The test class does not override the equals() method, so the memory address is compared by default, thus outputting obj1 not equals obj2.

洪涛

I think we should put aside the code implementation first and look directly at the meaning of the equals method - to compare whether the object is the same.
So the question is, how can objects be considered the same? This is decided by the author of the class to which the object belongs, and only he can decide how instances of the class he creates should compare identically.

I don’t know who the author of String is, but do you agree with his equals implementation? When comparing strings, you should compare whether the character sequences are the same.

As for test, its author is you, and you did not define how to compare the test object, but you called equals to compare it. What will be the result at this time? It can only be the equals result of the parent class of the test class. The default parent class is the Object class. @ch_gilbert has already explained~

阿神

equals depends on the specific implementation

public class A{ 
    private String color;
    private int weight;
     public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean equals(Object obj){
            if (obj == null)
                return false;
          if (this == obj)
                return true;
            if (getClass() != obj.getClass())
                return false;
            A other = (A) obj;
            if (color == null) {
                if (other.color != null)
                    return false;
            } else if (!color.equals(other.color))
                return false;
            if (weight != other.weight)
                return false;
            return true;
    }

}
伊谢尔伦

What you said is really complicated. After reading it, I should have a correct understanding
I'll post the source code of the Stirng.equals method, it's obvious.

 public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    }
You can understand the comparison of

==. equals To put it bluntly, isn’t it just a method? The exact result depends on how it is achieved, so why bother worrying about it?
Suppose equals is written like this

How about, == is different, it only returns true if it is "111111".

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            if (anotherString.equals("111111")) {
                return true;
            }
        }
        return false;
    }

Do you understand that? equalsThis thing is just a method! Because there is it in Object, other Classs inherit it, so it looks special.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template