Home >Java >JavaBase >What is the difference between equalsignorecase and equals?

What is the difference between equalsignorecase and equals?

青灯夜游
青灯夜游Original
2020-11-17 17:21:3212704browse

Difference: equals() is a method defined in the Object class. It determines whether two objects are "equal" and is case-sensitive; equalsIgnoreCase is a method defined in the string class and is used to compare two strings. Whether the corresponding characters in are equal, case will be ignored.

What is the difference between equalsignorecase and equals?

##The difference between equals() and equalsIgnoreCase() in JAVA

1. Use the equals() method to compare whether two strings are equal. It has the following general form:

boolean equals(Object str)

Here str is a String object used to compare with the calling String object. It returns true if two strings have the same characters and length, otherwise it returns false. This comparison is case-sensitive.

2. In order to perform a case-ignoring comparison, you can call the equalsIgnoreCase() method.

When comparing two strings, it will think A-Z and a-z are the same. Its general form is as follows:

boolean equalsIgnoreCase(String str)

Here, str is a String object used to compare with the calling String object. It also returns true if the two strings have the same characters and length, false otherwise. The following example illustrates the equals() and equalsIgnoreCase() methods:

// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " +
s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
}
}

The output of this program is as follows:

Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

For more programming-related knowledge, please visit:

Programming Learning ! !

The above is the detailed content of What is the difference between equalsignorecase and equals?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn