TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java String equals, equalsIgnoreCase and contentEquals

Use the String equals and equalsIgnoreCase methods. The equality operator and null are tested.
Equals, equalsIgnoreCase. Strings contain characters. These can be letters (lowercase and uppercase), digits, spaces. We want to compare two strings for equality.
With equals, we test the chars in two string objects. If all the characters are the same, equals() returns true. With equalsIgnoreCase, lowercase and uppercase are considered the same.

Warning: With the equality operator == only the references are compared, not the exact characters in two strings.

So: This can give incorrect results when two strings contain the same chars, but are not the same reference.

First example. Let us use equals() in a simple program. We introduce three test strings. They all contain the same characters, but the third one is not a string literal.

Result: The three strings are all considered equal—equals() returns true. But "456" is not equal to the test strings.

Java program that uses String equals public class Program { public static void main(String[] args) { String test = "1234"; String test2 = "1234"; String test3 = Integer.toString(1234); if (test.equals(test2)) { System.out.println(true); } if (test.equals(test3)) { System.out.println(true); } if (!test.equals("456")) { System.out.println(false); } } } Output true true false
Equals operator. The == and != operators can be used on string references. But this does not handle the internal characters in the String objects. It just tests the references.

Here: The two strings contain the same chars, but are not the same instance. Equals() returns true, but the == operator returns false.

Java program that tests equality operator, equals public class Program { public static void main(String[] args) { String test = "carrot1"; String test2 = "carrot" + Integer.toString(1); // Test the equals, not equals operator. if (test != test2) { System.out.println("Not equal"); System.out.println(test); System.out.println(test2); } // The equals method is different. if (test.equals(test2)) { System.out.println("Equals = true"); System.out.println(test); System.out.println(test2); } } } Output Not equal carrot1 carrot1 Equals = true carrot1 carrot1
EqualsIgnoreCase. Sometimes we do not care if a letter is uppercase or lower. The EqualsIgnoreCase method treats both cases as equal. It does not care about upper and lower.

Result: The program has a string "APPLE" and a string "apple." EqualsIgnoreCase returns true when we test those two strings.

Java program that uses equalsIgnoreCase public class Program { public static void main(String[] args) { String name = "APPLE"; String name2 = name.toLowerCase(); // The uppercase and lowercase letters are considered equal. if (name.equalsIgnoreCase(name2)) { System.out.println(true); System.out.println(name); System.out.println(name2); } } } Output true APPLE apple
Null. This program shows that a non-null string never equals a null string. Equals() will always return false when passed a null variable. But it throws no exception.
Java program that uses equals, null public class Program { public static void main(String[] args) { String value = "radish"; String other = null; // The null literal is not equal to any String. if (!value.equals(other)) { System.out.println(true); } } } Output true
ContentEquals. This method tests the value of objects through their CharSequences. So it can compare a String against a StringBuilder. It is called on a String instance.

Argument: We pass the other object to the contentEquals method. For example we can pass another String or a StringBuilder.

StringBuilder

Result: This returns true if the character data in both objects is equal. It returns false otherwise.

Tip: With contentEquals we can avoid converting some objects (like a StringBuilder) to a String for comparison—this improves performance.

Java program that uses contentEquals public class Program { public static void main(String[] args) { // Create 2 strings with the same character data. String part1 = "cat100"; String part2 = "cat"; part2 += String.valueOf(100); // Create a StringBuilder with the same character data. StringBuilder builder = new StringBuilder(); builder.append("cat"); builder.append(100); // Use contentEquals to compare character data. if (part1.contentEquals(part2)) { System.out.println("True 1"); } if (part1.contentEquals(builder)) { System.out.println("True 2"); } } } Output True 1 True 2
With Strings, we must keep in mind the difference between references and objects. A string variable is a reference, but this reference points to an object.
With equality operators (like "==") we can test two references for equality. But to check the data within String objects, we must use equals or equalsIgnoreCase.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf