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
how does default equals implementation in java works for String?
提问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 Object
class . But you can Override the equals
method in your own class to change the way equality
is done between two objects of the same class. For example the equals
method in String
class is overridden as follows:
是的,默认情况下 equals 方法==
在Object
class 中实现。但是您可以覆盖equals
您自己的类中的方法来更改equality
同一类的两个对象之间的完成方式。例如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;
}
So this is the reason that for the following code:
所以这就是以下代码的原因:
String s1 = new String("java");
String s2 = new String("java");
s1==s2
returns false
since both are referencing different objects on heap. Whereas s1.equals(s2)
returns true
since now the equals
being called is what defined within String
class where String
objects are compared on the basis of contents
of String.
s1==s2
返回,false
因为两者都引用了堆上的不同对象。而从现在起s1.equals(s2)
返回true
的equals
调用是在String
类中定义的,其中String
基于contents
String比较对象。
回答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
equals
method is originally a method of Object
class. And every class in Java extends the Object
class by default. Now, equals
method is overridden for String
class 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
String
class in java overrides the equals
method of Object
class such that it compares the content of the two strings rather than comparing the references(default implementation of in Object
class).
String
java中equals
的Object
类覆盖了类的方法,以便比较两个字符串的内容而不是比较引用(Object
类中的默认实现)。
See below the equals
method implementation of String
class:
见下面类的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;
}