Java String ComparisonLast Updated : 9 Jan 2026 In Java, strings can be compared based on their content or their references. String comparison checks whether two strings have the same sequence of characters, while reference comparison checks whether both variables point to the same object in memory. Java string comparison is commonly used for authentication using the equals() method, sorting using the compareTo() method, and reference matching using the == operator. The following are the different ways to compare strings:
Comparing Strings Using equals() MethodThe String class equals() method compares the original content of the string. It compares values of string for equality. String class provides the following two methods: Additionally, the equalsIgnoreCase() method performs a case-insensitive comparison. String class provides the following two methods:
![]() Example: Comparing Strings Without Ignoring CaseThe following example demonstrates string comparison using the equals() method without ignoring case. Output: true true false Explanation S1 and S2 relate to the same string pool object since they are string literals. As a result, s1.equals(s2) gives true. Despite S3 being developed with the new keyword, S1 and S3 had the same content. Since only the content is compared using the equals() method, s1.equals(s3) is true; however, s1.equals(s4) is false because the contents of s1 and s4 differ. In the above program, the methods of String class are used. The equals() method returns true if String objects are matching and both strings are of same case. equalsIgnoreCase() returns true regardless of cases of strings. Example: Comparing Strings With Ignoring CaseThe following example demonstrates string comparison using the equals() method with ignoring case. Output: false true Explanation There are two strings specified, s1 and s2, with distinct cases ("Ram" and "rAm"). Use the equals() function to compare s1 and s2. Because the cases are different, this method compares them in a case-sensitive fashion and returns false. The comparison between strings s1 and s2 is performed using the equalsIgnoreCase() function. Because it treats the material equally and ignores case distinctions, this function returns true. Comparing Strings Using Equal To (==) OperatorIn Java, the == operator compares references rather than values. ![]() ExampleThe following example demonstrates string comparison using the equal to (==) operator. Output: true false Comparing Strings Using compareTo() MethodThe String class compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string. Suppose s1 and s2 are two String objects. If:
ExampleThe following example demonstrates string comparison using the compareTo() method. Output: 0 1 -1 Explanation The content of str1 and str2 is the same-"Sachin," while str3 has the word "Ratan." Comparing Strings Using startsWith() and endsWith() MethodsIf a string starts with a prefix and ends with a suffix, respectively, it may be determined using the methods startsWith() and endsWith(). ExampleThe following example demonstrates string comparison using startsWith() and endsWith() methods. Output: true true Explanation startsWith("Hello") returns true if the string begins with the given prefix. endsWith("World!") returns true if the string does, in fact, conclude with the designated suffix. Next TopicString Concatenation in java ← prev next → |
We request you to subscribe our newsletter for upcoming updates.