Home > Java > JavaBase > body text

What is the difference between equalsignorecase and equals?

青灯夜游
Release: 2020-11-17 17:21:32
Original
12577 people have browsed it

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)
Copy after login

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)
Copy after login

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));
}
}
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!