java java中的默认等于实现如何适用于String?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15585573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:09:30  来源:igfitidea点击:

how does default equals implementation in java works for String?

javastringequals

提问by Praveen Kumar

We all know if we create two String objects and use == to compare them it will return false and if we use equals to method it will return true. But by default equals method implement == only , then how does it return true , it should return whatever == is returning ??

我们都知道如果我们创建两个 String 对象并使用 == 比较它们将返回 false,如果我们使用 equals to 方法它将返回 true。但默认情况下,equals 方法仅实现 == ,那么它如何返回 true ,它应该返回 == 返回的任何内容??

回答by Vishal K

Yes by default equals method implements ==in Objectclass . But you can Override the equalsmethod in your own class to change the way equalityis done between two objects of the same class. For example the equalsmethod in Stringclass is overridden as follows:

是的,默认情况下 equals 方法==Objectclass 中实现。但是您可以覆盖equals您自己的类中的方法来更改equality同一类的两个对象之间的完成方式。例如equalsString类中的方法被覆盖如下:

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

So this is the reason that for the following code:

所以这就是以下代码的原因:

String s1 = new String("java");
String s2 = new String("java");

s1==s2returns falsesince both are referencing different objects on heap. Whereas s1.equals(s2)returns truesince now the equalsbeing called is what defined within Stringclass where Stringobjects are compared on the basis of contentsof String.

s1==s2返回,false因为两者都引用了堆上的不同对象。而从现在起s1.equals(s2)返回trueequals调用是在String类中定义的,其中String基于contentsString比较对象。

回答by Ankur Shanbhag

the equals method in the String class is overridden and it tries to check if all the characters in both the Strings are equal or not. If found then it returns true. So the behavior of equals method in String class is different from the normal object class implementation of it.

String 类中的 equals 方法被覆盖,它尝试检查两个字符串中的所有字符是否相等。如果找到,则返回 true。所以 String 类中的 equals 方法的行为不同于它的正常对象类实现。

回答by Amar

equalsmethod is originally a method of Objectclass. And every class in Java extends the Objectclass by default. Now, equalsmethod is overridden for Stringclass to act differently than ==.

equals方法原本是Object类的方法。Java 中的每个类都Object默认扩展了该类。现在,equals方法被重写,String类的行为与==.

It's javadocexplains it perfectly:

它的javadoc完美地解释了它:

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

将此字符串与指定的对象进行比较。当且仅当参数不为 null 并且是表示与此对象相同的字符序列的 String 对象时,结果才为真。

It's implementation goes as follows:

它的实现如下:

@override
public boolean equals(Object anObject) {
// This check is just for the case when exact same String object is passed
if (this == anObject) {
    return true;
}
// After this only real implementation of equals start which you might be looking for
// For other cases checks start from here
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;
}

回答by Uko

.equals()checks if the strings are identical ei. have the same characters. ==only checks if pointers point to the same objects. You can have different objects with the same characters, thats why you should use .equals()to compare them

.equals()检查字符串是否相同 ei。有相同的字符。==只检查指针是否指向相同的对象。您可以拥有具有相同字符的不同对象,这就是您应该使用.equals()它们进行比较的原因

回答by Abubakkar

Stringclass in java overrides the equalsmethod of Objectclass such that it compares the content of the two strings rather than comparing the references(default implementation of in Objectclass).

Stringjava中equalsObject类覆盖了类的方法,以便比较两个字符串的内容而不是比较引用(Object类中的默认实现)。

See below the equalsmethod implementation of Stringclass:

见下面类的equals方法实现String

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