String.equals() in Java compares the content (character sequence) of two strings, returning true if they are identical and false otherwise. It is the standard method for checking if two strings have the same value.
Syntax:
public boolean equals(Object anotherObject)Key Points:
Compares character-by-character, ignoring object memory location.
Returns
falseif the string isnull(safe fromNullPointerException).Case-sensitive; use
equalsIgnoreCase()for case-insensitive comparison.Always preferred over
==for content comparison, as==checks reference equality (same object in memory).
Example:
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // true (same content)
System.out.println(str1 == str2); // false (different objects)For robust comparison, especially when null values are possible, use Objects.equals(str1, str2) (available from JDK 7).
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they contain the same data).
Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
From the Java Language Specification JLS 15.21.3. Reference Equality Operators == and !=:
While
==may be used to compare references of typeString, such an equality test determines whether or not the two operands refer to the sameStringobject. The result isfalseif the operands are distinctStringobjects, even if they contain the same sequence of characters (§3.10.5, §3.10.6). The contents of two stringssandtcan be tested for equality by the method invocations.equals(t).
You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern.
Similar examples can also be found in JLS 3.10.5-1.
Other Methods To Consider
String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.
String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they contain the same data).
Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
From the Java Language Specification JLS 15.21.3. Reference Equality Operators == and !=:
While
==may be used to compare references of typeString, such an equality test determines whether or not the two operands refer to the sameStringobject. The result isfalseif the operands are distinctStringobjects, even if they contain the same sequence of characters (§3.10.5, §3.10.6). The contents of two stringssandtcan be tested for equality by the method invocations.equals(t).
You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern.
Similar examples can also be found in JLS 3.10.5-1.
Other Methods To Consider
String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.
String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.
== tests object references, .equals() tests the string values.
Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
== handles null strings fine, but calling .equals() from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1 may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following are shorter, but it’s less obvious that it checks for null:
System.out.print("bar".equals(fooString1)); // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar")); // Java 7 required
Java string equals string in if statement
How does Java string compare work, and when should you use `==` vs. `.equals()`? - TestMu AI Community
Java. Is there a String that is not equal to itself?
This may crash or may not, depending on how the constructor of B is defined. Maybe the constructor of B sets name to a random word every time the constructor is called or maybe it's always the same.
Opposite of .equals in Java?
Videos
My CS teacher told me that you have to use .equals() when comparing strings instead of == but today I tried using == and it worked. Was this supposed to happen or was it like a new update?
public class Main{
public static void main(String[] args) {
String name = "jarod";
if(name == "jarod"){
System.out.println("ELLO JAROD");
}else{
System.out.print("NO");
}
}
}For this bit of code the output was ello Jarod and if I put in something else for the string variable it gave me no