What is the difference between the String#equals
method and the String#contentEquals
method?
字符串#equals方法和字符串#contentEquals方法之间的区别是什么?
10 个解决方案
#1
122
The String#equals()
not only compares the String's contents, but also checks if the other object is also an instance of a String
. The String#contentEquals()
only compares the contents (the character sequence) and does not check if the other object is also an instance of String
. It can be anything as long as it is an implementation of CharSequence
which covers a.o. String
, StringBuilder
, StringBuffer
, CharBuffer
, etc.
String#equals()不仅比较字符串的内容,还检查其他对象是否也是字符串的实例。String#contentEquals()只比较内容(字符序列),不检查其他对象是否也是字符串的实例。它可以是任何东西,只要它是CharSequence的实现,它涵盖a.o. String、StringBuilder、StringBuffer、CharBuffer等。
#2
30
To put it easily: String.contentEquals()
is the smarter brother of String.equals()
, because it can be more free in the implementation than String.equals()
.
简单地说:String.contentEquals()是String.equals()的更聪明的兄弟,因为它在实现中比String.equals()更*。
There are some reasons why there is a separate String.contentEquals()
method. The most important reason I think is:
contentequals()方法是有原因的。我认为最重要的原因是:
- The
equals
method has to be reflexive. That means that:x.equals(y) == y.equals(x)
. This implies thataString.equals(aStringBuffer)
would have to be the same asaStringBuffer.equals(aString)
. This would require the Java API developers to make some special implementation for Strings in theequals()
method of StringBuffer, StringBuilder and CharSequence as well. This would be a mess. - 等号的方法必须是自反的。这意味着:x。= (y) = y。这意味着,astr. equals(aStringBuffer)必须与aStringBuffer.equals(aString)相同。这需要Java API开发人员在StringBuffer、StringBuilder和CharSequence的equals()方法中为字符串做一些特殊的实现。这将是一团糟。
So, and that is when String.contentEquals
comes in. This is a standalone method that does not have to follow the strict requirements and rules for Object.equals
. This way, you can implement the sense of "equal content" more freely. This allows you to make intelligent comparisons between a StringBuffer and a String, for example.
这就是字符串。contentEquals出现的原因。这是一个独立的方法,不需要遵循Object.equals的严格要求和规则。这样,你就可以更*地实现“平等内容”的感觉。例如,这允许您在StringBuffer和String之间进行智能比较。
And to say what exactly the difference is:
我想说的是
-
String.contentEquals()
can compare the contents of aString
, aStringBuilder
, aStringBuffer
, aCharSequence
and all derivate classes of these. If the parameter is of type String, thenString.equals()
get executed.contentequals()可以比较字符串、StringBuilder、StringBuffer、CharSequence以及它们的所有派生类的内容。如果参数是String,则String.equals()将被执行。
-
String.equals()
only compares String objects. All other object types are considered as not equal.=()只比较字符串对象。所有其他对象类型都被认为是不相等的。
-
String.contentEquals()
can compareStringBuffer
andStringBuilder
in an intelligent way. It does not call the heavytoString()
method, which copies the whole content to a new String object. Instead, it compares with the underlyingchar[]
array, which is great.contentequals()可以以一种智能的方式比较StringBuffer和StringBuilder。它不调用heavy toString()方法,该方法将整个内容复制到一个新的String对象。相反,它与底层的char[]数组进行比较,这很好。
#3
25
This answer was already posted by dbw but he deleted it but he had some very valid points for the difference while comparing execution time, what exceptions are thrown,
这个答案已经被dbw发布了但是他删除了它但是他有一些非常有效的点在比较执行时间的时候,抛出了哪些异常,
If you look at the source code String#equals and String#contentEquals it is clear that there are two overridden methods for String#contentEquals
one which take StringBuilder
and other CharSequence
.
The difference between them,
如果您查看源代码字符串#equals和String#contentEquals,很明显,字符串#contentEquals有两个重写的方法,一个使用StringBuilder,另一个使用CharSequence。他们之间的区别,
-
String#contentEquals
will throw NPE if the argument supplied isnull
butString#equals
will returnfalse
- 如果提供的参数为空,那么String#contentEquals将抛出NPE,但String#equals将返回false
-
String#equals
compares the content only when the argument supplied isinstance of String
otherwise it will returnfalse
in all other cases but on the other handString#contentEquals
checks the content of all the objects which implement interfaceCharSequence
. - String# =只在提供的参数是String的实例时比较内容,否则在所有其他情况下都会返回false,但另一方面String#contentEquals检查实现接口CharSequence的所有对象的内容。
-
You can also tweak the code so that
String#contentEquals
return the wrong result or result you want by overridingequals
method of the argument passed as shown below but you can not do those tweaks withString#equals
.
Below code will always producetrue
as long ass
contains anystring
which is 3 character long您还可以调整代码,使字符串#contentEquals通过重写传递的参数的equals方法(如下所示)返回您想要的错误结果或结果,但是您不能使用String#equals方法进行这些修改。只要s包含任何3个字符长的字符串,下面的代码将始终生成true
String s= new String("abc");// "abc"; System.out.println(s.contentEquals(new CharSequence() { @Override public CharSequence subSequence(int arg0, int arg1) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } @Override public char charAt(int arg0) { // TODO Auto-generated method stub return 0; } @Override public boolean equals(Object obj) { return true; } }));
-
String#contentEquals
will be slower thenString#Equals
in the case when argument supplied isinstance of String
and the length of bothString
is same but contents are not equal.
Example if the string areString s = "madam"
andString argPassed = "madan"
thens.contentEquals(argPassed)
will take almost double execution time in this case as compared tos.equals(argPassed)
如果提供的参数是String的实例,并且两个字符串的长度相同但内容不相等,那么String#contentEquals将会比String#Equals慢。例如,如果字符串是字符串s = "madam"和字符串argpass = "madan",那么与s.equals(argpass)相比,s. contentequals (argpass)将花费几乎双倍的执行时间
-
If the content length are not same for both the strings then function
String#contentEquals
will have better performance thenString#Equals
in almost all possible cases.如果两个字符串的内容长度不相同,那么在几乎所有可能的情况下,字符串#contentEquals将具有更好的性能。
One more point to add to his answer
还有一点要补充他的回答。
-
String#contentEquals
of aString
object will also compare to theStringBuilder
contents and provide the appropriate result whileString#Equals
will returnfalse
- String对象的String#contentEquals也将与StringBuilder的内容进行比较,并提供适当的结果,而String#Equals将返回false。
#4
12
contentEquals(CharSequence cs)
:
contentEquals(CharSequence进行cs):
- Lets you check equality of given string value with any implementation instance of interface
java.lang.CharacterSequence
(eg,CharBuffer
,Segment
,String
,StringBuffer
,StringBuilder
) - 让您检查给定字符串值与接口java.lang的任何实现实例是否相等。字符序列(如CharBuffer、Segment、String、StringBuffer、StringBuilder)
equals(Object anObject)
:
anObject equals(对象):
- Lets you check equality of given string value with any instance of type
java.lang.String
only - 让您检查给定的字符串值是否与java.lang类型的任何实例相等。字符串只
RTFC :)
RTFC:)
Since reading the source is the best way to understand it, I am sharing the implementations of both the methods (as of jdk 1.7.0_45)
因为阅读源代码是理解它的最佳方式,所以我将共享这两种方法的实现(从jdk 1.7.0_45开始)
public boolean contentEquals(CharSequence cs) {
if (value.length != cs.length())
return false;
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
char v1[] = value;
char v2[] = ((AbstractStringBuilder) cs).getValue();
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
// Argument is a String
if (cs.equals(this))
return true;
// Argument is a generic CharSequence
char v1[] = value;
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != cs.charAt(i))
return false;
i++;
}
return 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;
}
There is another method of String#contentEquals():
字符串#contentEquals()还有另一种方法:
public boolean contentEquals(StringBuffer sb) {
synchronized(sb) {
return contentEquals((CharSequence)sb);
}
}
#5
8
-
String
classequals(Object o)
method does onlyString
comparison. ButcontentEquals(CharSequence cs)
checks for classes extendsAbstractStringBuilder
i.e.StringBuffer
,StringBuilder
andString
class also (They all are of typeCharSequence
).String class = (Object o)方法只做字符串比较。但是contentEquals(CharSequence cs)检查类扩展了AbstractStringBuilder,即StringBuffer、StringBuilder和String类(它们都是类型CharSequence)。
String str = "*"; StringBuilder builder = new StringBuilder(str); System.out.println(str.equals(builder)); System.out.println(str.contentEquals(builder));
output:
输出:
false
true
The output of first stmt is false
because builder
is not of type String
so equals()
returns false
but the contentEquals()
checks for the content of all the type like StringBuilder
, StringBuffer
, String
and as the content is same hence true
.
第一个stmt的输出是false,因为builder不是String类型的,所以equals()返回false,但是contentEquals()检查所有类型的内容,如StringBuilder、StringBuffer、String,因为内容相同,因此为true。
-
contentEquals
will throwNullPointerException
if the argument supplied isnull
butequals()
will return false because the equals() checks for instanceOf (if (anObject instance of String)
) which returns false if the argument isnull
. - 如果提供的参数为空,contentEquals将抛出NullPointerException,但equals()将返回false,因为equals()检查instanceOf (if (String的anObject实例)),如果参数为空,则equals()将返回false。
#6
6
equals()
and contentEquals()
are two methods in String
class to compare two strings
and string
with StringBuffer
.
equals()和contentEquals()是String类中的两个方法,用于比较两个字符串和StringBuffer的字符串。
The parameters of contentEquals()
are StringBuffer
and String(charSequence)
. equals()
is used to compare two strings
and contentEquals()
is used to compare the contents of String
and StringBuffer
.
contentEquals()的参数是StringBuffer和String(charSequence)。equals()用于比较两个字符串,contentEquals()用于比较字符串和StringBuffer的内容。
Method contentEquals
and equals
are
方法contentEquals和equals
public boolean contentEquals(java.lang.StringBuffer);
public boolean contentEquals(java.lang.CharSequence);
public boolean equals(Object o)
Here is an code which describes both methods
下面是描述这两种方法的代码
public class compareString {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
boolean result1 = str1.equals(str2); // works nice and returns true
System.out.println(" str1.equals(str2) - "+ result1);
boolean result2 = str1.equals(sb1); // works nice and returns false
System.out.println(" str1.equals(sb1) - "+ result2);
boolean result3 = str1.contentEquals(sb1); // works nice and returns true
System.out.println(" str1.contentEquals(sb1) - "+ result3);
boolean result4 = str1.contentEquals(sb2); // works nice and returns false
System.out.println(" str1.contentEquals(sb2) - "+ result4);
boolean result5 = str1.contentEquals(str2); // works nice and returns true
System.out.println(" str1.contentEquals(str2) - "+ result5);
}
}
Output:
输出:
str1.equals(str2) - true
str1.equals(sb1) - false
str1.contentEquals(sb1) - true
str1.contentEquals(sb2) - false
str1.contentEquals(str2) - true
#7
5
The contentEquals()
method checks is the contents are same between a String
, StringBuffer
, etc which some kind of char sequence.
contentEquals()方法检查的是字符串、StringBuffer等之间的内容是相同的,它们是某种char序列。
#8
5
String#equals takes Object as an argument and checks it is instance of String object or not. If the argument object is String Object then it compares content character by character. It returns true in case content of both string objects are same.
String#equals接受Object作为一个参数,并检查它是否是String Object的实例。如果参数对象是String对象,那么它将按字符对内容进行比较。如果两个字符串对象的内容相同,则返回true。
String#contentEquals takes CharSequence interface as an argument. CharSequence can be implements in 2 ways-by using i) String class or (ii) AbstractStringBuilder( parent class of StringBuffer, StringBuilder)
字符串#contentEquals将CharSequence接口作为参数。可以通过两种方式实现CharSequence—通过使用i) String类或(ii) AbstractStringBuilder(StringBuffer的父类、StringBuilder)
In contentEquals() length is compared before any object instance check. If length is same then it checks argument object is instance of AbstractStringBuilder or not. If it is so(i.e. StringBuffer or StringBuilder ) then content is checked character by character. In case argument is an instance of String object then String#equals called from String#contentEquals.
在contentEquals()长度在任何对象实例检查之前进行比较。如果长度相同,则检查参数对象是否是AbstractStringBuilder的实例。如果它(即。然后按字符检查内容。如果参数是String对象的实例,那么String# =就会从String#contentEquals中调用。
So in short,
简言之,
String#equals compares the content character by character in case argument is String object also. And String#contentEquals compares the content in case argument object implement CharSequence interface.
String# =按字符比较内容字符,如果参数也是String对象。而字符串#contentEquals则比较case参数对象实现CharSequence接口的内容。
String#contentEquals is slower in case we compare two same length string content as String#contentEquals internally calls String#equals for String object.
如果我们比较两个相同长度的字符串内容,如String#contentEquals,在内部调用String# = for String对象时,字符串#contentEquals的速度会慢一些。
In case we try to compare objects with difference content length (say "abc" with "abcd") then String#contentEquals is faster than String#equals. Because length is compared before any object instance checking.
如果我们尝试比较不同内容长度的对象(比如“abc”和“abcd”),那么String#contentEquals将比String#equals更快。因为长度是在任何对象实例检查之前进行比较的。
#9
4
BTW, the historical reason for the difference is that String originally had no superclass, so String.equals() takes a String as its argument. When CharSequence was introduced as the superclass of String, it needed an equality test of its own that worked across all CharSequence implementations, and that would not collide with the equals() already in use by String... so we got CharSequence.contentEquals(), which is inherited by String.
顺便说一句,这种差异的历史原因是字符串最初没有超类,所以String.equals()将一个字符串作为它的参数。当CharSequence作为String的超类引入时,它需要自己进行一个等式测试,该测试可以在所有的CharSequence实现中工作,并且不会与String已经在使用的equals()发生冲突……我们得到了CharSequence.contentEquals(),它是由字符串继承的。
If CharSequence has been present in Java 1.0, we would probalby have only CharSequence.equals() and String would simply implement that.
如果CharSequence已经出现在Java 1.0中,那么我们将probalby只有CharSequence.equals(),而String仅仅实现它。
Ah, the joys of evolving languages...
啊,进化语言的乐趣……
#10
1
The big difference is that equals() will only work with another String, while contentEquals() would work on any CharacterSequence (like StringBuilder).
最大的区别是,equals()将只使用另一个字符串,而contentEquals()将处理任何字符序列(比如StringBuilder)。
public class Test{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = new String("Hello");
StringBuilder str3 = new StringBuilder(str1);
System.out.println("str1.equals(str2): " + (str1.equals(str2)));
System.out.println("str1.contentEquals(str2): " + (str1.contentEquals(str2)));
System.out.println("str1.equals(str3): " + (str1.equals(str3)));
System.out.println("str1.contentEquals(str3): " + (str1.contentEquals(str3)));
}}
will give output as
将输出作为
str1.equals(str2): true
str1.equals(str2):真的
str1.contentEquals(str2): true
str1.contentEquals(str2):真的
str1.equals(str3): false
str1.equals(str3):假的
str1.contentEquals(str3): true
str1.contentEquals(str3):真的
#1
122
The String#equals()
not only compares the String's contents, but also checks if the other object is also an instance of a String
. The String#contentEquals()
only compares the contents (the character sequence) and does not check if the other object is also an instance of String
. It can be anything as long as it is an implementation of CharSequence
which covers a.o. String
, StringBuilder
, StringBuffer
, CharBuffer
, etc.
String#equals()不仅比较字符串的内容,还检查其他对象是否也是字符串的实例。String#contentEquals()只比较内容(字符序列),不检查其他对象是否也是字符串的实例。它可以是任何东西,只要它是CharSequence的实现,它涵盖a.o. String、StringBuilder、StringBuffer、CharBuffer等。
#2
30
To put it easily: String.contentEquals()
is the smarter brother of String.equals()
, because it can be more free in the implementation than String.equals()
.
简单地说:String.contentEquals()是String.equals()的更聪明的兄弟,因为它在实现中比String.equals()更*。
There are some reasons why there is a separate String.contentEquals()
method. The most important reason I think is:
contentequals()方法是有原因的。我认为最重要的原因是:
- The
equals
method has to be reflexive. That means that:x.equals(y) == y.equals(x)
. This implies thataString.equals(aStringBuffer)
would have to be the same asaStringBuffer.equals(aString)
. This would require the Java API developers to make some special implementation for Strings in theequals()
method of StringBuffer, StringBuilder and CharSequence as well. This would be a mess. - 等号的方法必须是自反的。这意味着:x。= (y) = y。这意味着,astr. equals(aStringBuffer)必须与aStringBuffer.equals(aString)相同。这需要Java API开发人员在StringBuffer、StringBuilder和CharSequence的equals()方法中为字符串做一些特殊的实现。这将是一团糟。
So, and that is when String.contentEquals
comes in. This is a standalone method that does not have to follow the strict requirements and rules for Object.equals
. This way, you can implement the sense of "equal content" more freely. This allows you to make intelligent comparisons between a StringBuffer and a String, for example.
这就是字符串。contentEquals出现的原因。这是一个独立的方法,不需要遵循Object.equals的严格要求和规则。这样,你就可以更*地实现“平等内容”的感觉。例如,这允许您在StringBuffer和String之间进行智能比较。
And to say what exactly the difference is:
我想说的是
-
String.contentEquals()
can compare the contents of aString
, aStringBuilder
, aStringBuffer
, aCharSequence
and all derivate classes of these. If the parameter is of type String, thenString.equals()
get executed.contentequals()可以比较字符串、StringBuilder、StringBuffer、CharSequence以及它们的所有派生类的内容。如果参数是String,则String.equals()将被执行。
-
String.equals()
only compares String objects. All other object types are considered as not equal.=()只比较字符串对象。所有其他对象类型都被认为是不相等的。
-
String.contentEquals()
can compareStringBuffer
andStringBuilder
in an intelligent way. It does not call the heavytoString()
method, which copies the whole content to a new String object. Instead, it compares with the underlyingchar[]
array, which is great.contentequals()可以以一种智能的方式比较StringBuffer和StringBuilder。它不调用heavy toString()方法,该方法将整个内容复制到一个新的String对象。相反,它与底层的char[]数组进行比较,这很好。
#3
25
This answer was already posted by dbw but he deleted it but he had some very valid points for the difference while comparing execution time, what exceptions are thrown,
这个答案已经被dbw发布了但是他删除了它但是他有一些非常有效的点在比较执行时间的时候,抛出了哪些异常,
If you look at the source code String#equals and String#contentEquals it is clear that there are two overridden methods for String#contentEquals
one which take StringBuilder
and other CharSequence
.
The difference between them,
如果您查看源代码字符串#equals和String#contentEquals,很明显,字符串#contentEquals有两个重写的方法,一个使用StringBuilder,另一个使用CharSequence。他们之间的区别,
-
String#contentEquals
will throw NPE if the argument supplied isnull
butString#equals
will returnfalse
- 如果提供的参数为空,那么String#contentEquals将抛出NPE,但String#equals将返回false
-
String#equals
compares the content only when the argument supplied isinstance of String
otherwise it will returnfalse
in all other cases but on the other handString#contentEquals
checks the content of all the objects which implement interfaceCharSequence
. - String# =只在提供的参数是String的实例时比较内容,否则在所有其他情况下都会返回false,但另一方面String#contentEquals检查实现接口CharSequence的所有对象的内容。
-
You can also tweak the code so that
String#contentEquals
return the wrong result or result you want by overridingequals
method of the argument passed as shown below but you can not do those tweaks withString#equals
.
Below code will always producetrue
as long ass
contains anystring
which is 3 character long您还可以调整代码,使字符串#contentEquals通过重写传递的参数的equals方法(如下所示)返回您想要的错误结果或结果,但是您不能使用String#equals方法进行这些修改。只要s包含任何3个字符长的字符串,下面的代码将始终生成true
String s= new String("abc");// "abc"; System.out.println(s.contentEquals(new CharSequence() { @Override public CharSequence subSequence(int arg0, int arg1) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } @Override public char charAt(int arg0) { // TODO Auto-generated method stub return 0; } @Override public boolean equals(Object obj) { return true; } }));
-
String#contentEquals
will be slower thenString#Equals
in the case when argument supplied isinstance of String
and the length of bothString
is same but contents are not equal.
Example if the string areString s = "madam"
andString argPassed = "madan"
thens.contentEquals(argPassed)
will take almost double execution time in this case as compared tos.equals(argPassed)
如果提供的参数是String的实例,并且两个字符串的长度相同但内容不相等,那么String#contentEquals将会比String#Equals慢。例如,如果字符串是字符串s = "madam"和字符串argpass = "madan",那么与s.equals(argpass)相比,s. contentequals (argpass)将花费几乎双倍的执行时间
-
If the content length are not same for both the strings then function
String#contentEquals
will have better performance thenString#Equals
in almost all possible cases.如果两个字符串的内容长度不相同,那么在几乎所有可能的情况下,字符串#contentEquals将具有更好的性能。
One more point to add to his answer
还有一点要补充他的回答。
-
String#contentEquals
of aString
object will also compare to theStringBuilder
contents and provide the appropriate result whileString#Equals
will returnfalse
- String对象的String#contentEquals也将与StringBuilder的内容进行比较,并提供适当的结果,而String#Equals将返回false。
#4
12
contentEquals(CharSequence cs)
:
contentEquals(CharSequence进行cs):
- Lets you check equality of given string value with any implementation instance of interface
java.lang.CharacterSequence
(eg,CharBuffer
,Segment
,String
,StringBuffer
,StringBuilder
) - 让您检查给定字符串值与接口java.lang的任何实现实例是否相等。字符序列(如CharBuffer、Segment、String、StringBuffer、StringBuilder)
equals(Object anObject)
:
anObject equals(对象):
- Lets you check equality of given string value with any instance of type
java.lang.String
only - 让您检查给定的字符串值是否与java.lang类型的任何实例相等。字符串只
RTFC :)
RTFC:)
Since reading the source is the best way to understand it, I am sharing the implementations of both the methods (as of jdk 1.7.0_45)
因为阅读源代码是理解它的最佳方式,所以我将共享这两种方法的实现(从jdk 1.7.0_45开始)
public boolean contentEquals(CharSequence cs) {
if (value.length != cs.length())
return false;
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
char v1[] = value;
char v2[] = ((AbstractStringBuilder) cs).getValue();
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
// Argument is a String
if (cs.equals(this))
return true;
// Argument is a generic CharSequence
char v1[] = value;
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != cs.charAt(i))
return false;
i++;
}
return 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;
}
There is another method of String#contentEquals():
字符串#contentEquals()还有另一种方法:
public boolean contentEquals(StringBuffer sb) {
synchronized(sb) {
return contentEquals((CharSequence)sb);
}
}
#5
8
-
String
classequals(Object o)
method does onlyString
comparison. ButcontentEquals(CharSequence cs)
checks for classes extendsAbstractStringBuilder
i.e.StringBuffer
,StringBuilder
andString
class also (They all are of typeCharSequence
).String class = (Object o)方法只做字符串比较。但是contentEquals(CharSequence cs)检查类扩展了AbstractStringBuilder,即StringBuffer、StringBuilder和String类(它们都是类型CharSequence)。
String str = "*"; StringBuilder builder = new StringBuilder(str); System.out.println(str.equals(builder)); System.out.println(str.contentEquals(builder));
output:
输出:
false
true
The output of first stmt is false
because builder
is not of type String
so equals()
returns false
but the contentEquals()
checks for the content of all the type like StringBuilder
, StringBuffer
, String
and as the content is same hence true
.
第一个stmt的输出是false,因为builder不是String类型的,所以equals()返回false,但是contentEquals()检查所有类型的内容,如StringBuilder、StringBuffer、String,因为内容相同,因此为true。
-
contentEquals
will throwNullPointerException
if the argument supplied isnull
butequals()
will return false because the equals() checks for instanceOf (if (anObject instance of String)
) which returns false if the argument isnull
. - 如果提供的参数为空,contentEquals将抛出NullPointerException,但equals()将返回false,因为equals()检查instanceOf (if (String的anObject实例)),如果参数为空,则equals()将返回false。
#6
6
equals()
and contentEquals()
are two methods in String
class to compare two strings
and string
with StringBuffer
.
equals()和contentEquals()是String类中的两个方法,用于比较两个字符串和StringBuffer的字符串。
The parameters of contentEquals()
are StringBuffer
and String(charSequence)
. equals()
is used to compare two strings
and contentEquals()
is used to compare the contents of String
and StringBuffer
.
contentEquals()的参数是StringBuffer和String(charSequence)。equals()用于比较两个字符串,contentEquals()用于比较字符串和StringBuffer的内容。
Method contentEquals
and equals
are
方法contentEquals和equals
public boolean contentEquals(java.lang.StringBuffer);
public boolean contentEquals(java.lang.CharSequence);
public boolean equals(Object o)
Here is an code which describes both methods
下面是描述这两种方法的代码
public class compareString {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
boolean result1 = str1.equals(str2); // works nice and returns true
System.out.println(" str1.equals(str2) - "+ result1);
boolean result2 = str1.equals(sb1); // works nice and returns false
System.out.println(" str1.equals(sb1) - "+ result2);
boolean result3 = str1.contentEquals(sb1); // works nice and returns true
System.out.println(" str1.contentEquals(sb1) - "+ result3);
boolean result4 = str1.contentEquals(sb2); // works nice and returns false
System.out.println(" str1.contentEquals(sb2) - "+ result4);
boolean result5 = str1.contentEquals(str2); // works nice and returns true
System.out.println(" str1.contentEquals(str2) - "+ result5);
}
}
Output:
输出:
str1.equals(str2) - true
str1.equals(sb1) - false
str1.contentEquals(sb1) - true
str1.contentEquals(sb2) - false
str1.contentEquals(str2) - true
#7
5
The contentEquals()
method checks is the contents are same between a String
, StringBuffer
, etc which some kind of char sequence.
contentEquals()方法检查的是字符串、StringBuffer等之间的内容是相同的,它们是某种char序列。
#8
5
String#equals takes Object as an argument and checks it is instance of String object or not. If the argument object is String Object then it compares content character by character. It returns true in case content of both string objects are same.
String#equals接受Object作为一个参数,并检查它是否是String Object的实例。如果参数对象是String对象,那么它将按字符对内容进行比较。如果两个字符串对象的内容相同,则返回true。
String#contentEquals takes CharSequence interface as an argument. CharSequence can be implements in 2 ways-by using i) String class or (ii) AbstractStringBuilder( parent class of StringBuffer, StringBuilder)
字符串#contentEquals将CharSequence接口作为参数。可以通过两种方式实现CharSequence—通过使用i) String类或(ii) AbstractStringBuilder(StringBuffer的父类、StringBuilder)
In contentEquals() length is compared before any object instance check. If length is same then it checks argument object is instance of AbstractStringBuilder or not. If it is so(i.e. StringBuffer or StringBuilder ) then content is checked character by character. In case argument is an instance of String object then String#equals called from String#contentEquals.
在contentEquals()长度在任何对象实例检查之前进行比较。如果长度相同,则检查参数对象是否是AbstractStringBuilder的实例。如果它(即。然后按字符检查内容。如果参数是String对象的实例,那么String# =就会从String#contentEquals中调用。
So in short,
简言之,
String#equals compares the content character by character in case argument is String object also. And String#contentEquals compares the content in case argument object implement CharSequence interface.
String# =按字符比较内容字符,如果参数也是String对象。而字符串#contentEquals则比较case参数对象实现CharSequence接口的内容。
String#contentEquals is slower in case we compare two same length string content as String#contentEquals internally calls String#equals for String object.
如果我们比较两个相同长度的字符串内容,如String#contentEquals,在内部调用String# = for String对象时,字符串#contentEquals的速度会慢一些。
In case we try to compare objects with difference content length (say "abc" with "abcd") then String#contentEquals is faster than String#equals. Because length is compared before any object instance checking.
如果我们尝试比较不同内容长度的对象(比如“abc”和“abcd”),那么String#contentEquals将比String#equals更快。因为长度是在任何对象实例检查之前进行比较的。
#9
4
BTW, the historical reason for the difference is that String originally had no superclass, so String.equals() takes a String as its argument. When CharSequence was introduced as the superclass of String, it needed an equality test of its own that worked across all CharSequence implementations, and that would not collide with the equals() already in use by String... so we got CharSequence.contentEquals(), which is inherited by String.
顺便说一句,这种差异的历史原因是字符串最初没有超类,所以String.equals()将一个字符串作为它的参数。当CharSequence作为String的超类引入时,它需要自己进行一个等式测试,该测试可以在所有的CharSequence实现中工作,并且不会与String已经在使用的equals()发生冲突……我们得到了CharSequence.contentEquals(),它是由字符串继承的。
If CharSequence has been present in Java 1.0, we would probalby have only CharSequence.equals() and String would simply implement that.
如果CharSequence已经出现在Java 1.0中,那么我们将probalby只有CharSequence.equals(),而String仅仅实现它。
Ah, the joys of evolving languages...
啊,进化语言的乐趣……
#10
1
The big difference is that equals() will only work with another String, while contentEquals() would work on any CharacterSequence (like StringBuilder).
最大的区别是,equals()将只使用另一个字符串,而contentEquals()将处理任何字符序列(比如StringBuilder)。
public class Test{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = new String("Hello");
StringBuilder str3 = new StringBuilder(str1);
System.out.println("str1.equals(str2): " + (str1.equals(str2)));
System.out.println("str1.contentEquals(str2): " + (str1.contentEquals(str2)));
System.out.println("str1.equals(str3): " + (str1.equals(str3)));
System.out.println("str1.contentEquals(str3): " + (str1.contentEquals(str3)));
}}
will give output as
将输出作为
str1.equals(str2): true
str1.equals(str2):真的
str1.contentEquals(str2): true
str1.contentEquals(str2):真的
str1.equals(str3): false
str1.equals(str3):假的
str1.contentEquals(str3): true
str1.contentEquals(str3):真的