String is a sequence of characters. In java, objects of String are immutable which means they are constant and cannot be changed once created.
Below are 5 ways to compare two Strings in Java:
- Using user-defined function : Define a function to compare values with following conditions :
- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
i.e.(string1 == string2) it returns 0. - if (string1 < string2) it returns a negative value.
The value is calculated as (int)str1.charAt(i) – (int)str2.charAt(i)
Examples:
Input 1: GeeksforGeeks Input 2: Practice Output: -9 Input 1: Geeks Input 2: Geeks Output: 0 Input 1: GeeksforGeeks Input 2: Geeks Output: 8
Program:
// Java program to Compare two strings// lexicographicallypublicclassGFG {// This method compares two strings// lexicographically without using// library functionspublicstaticintstringCompare(String str1, String str2){intl1 = str1.length();intl2 = str2.length();for(inti =0; i < l1 && i < l2; i++) {intstr1_ch = (int)str1.charAt(i);intstr2_ch = (int)str2.charAt(i);if(str1_ch == str2_ch) {continue;}else{returnstr1_ch - str2_ch;}}// Edge case for strings like// String 1="Geeks" and String 2="Geeksforgeeks"if(l1 < l2) {returnl1 - l2;}elseif(l1 > l2) {returnl1 - l2;}// If none of the above conditions is true,// it implies both the strings are equalelse{return0;}}// Driver function to test the above programpublicstaticvoidmain(String args[]){String string1 =newString("Geeksforgeeks");String string2 =newString("Practice");String string3 =newString("Geeks");String string4 =newString("Geeks");// Comparing for String 1 < String 2System.out.println("Comparing "+ string1 +" and "+ string2+" : "+ stringCompare(string1, string2));// Comparing for String 3 = String 4System.out.println("Comparing "+ string3 +" and "+ string4+" : "+ stringCompare(string3, string4));// Comparing for String 1 > String 4System.out.println("Comparing "+ string1 +" and "+ string4+" : "+ stringCompare(string1, string4));}}chevron_rightfilter_noneOutput:Comparing Geeksforgeeks and Practice : -9 Comparing Geeks and Geeks : 0 Comparing Geeksforgeeks and Geeks : 8
- Using String.equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If all characters do not match, then it returns false.
Syntax:
str1.equals(str2);
Here str1 and str2 both are the strings which are to be compared.
Examples:
Input 1: GeeksforGeeks Input 2: Practice Output: false Input 1: Geeks Input 2: Geeks Output: true Input 1: geeks Input 2: Geeks Output: false
Program:
// Java program to Compare two strings// lexicographicallypublicclassGFG {publicstaticvoidmain(String args[]){String string1 =newString("Geeksforgeeks");String string2 =newString("Practice");String string3 =newString("Geeks");String string4 =newString("Geeks");String string5 =newString("geeks");// Comparing for String 1 != String 2System.out.println("Comparing "+ string1 +" and "+ string2+" : "+ string1.equals(string2));// Comparing for String 3 = String 4System.out.println("Comparing "+ string3 +" and "+ string4+" : "+ string3.equals(string4));// Comparing for String 4 != String 5System.out.println("Comparing "+ string4 +" and "+ string5+" : "+ string4.equals(string5));// Comparing for String 1 != String 4System.out.println("Comparing "+ string1 +" and "+ string4+" : "+ string1.equals(string4));}}chevron_rightfilter_noneOutput:Comparing Geeksforgeeks and Practice : false Comparing Geeks and Geeks : true Comparing Geeks and geeks : false Comparing Geeksforgeeks and Geeks : false
- Using String.equalsIgnoreCase() : The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and it returns an equivalent String ignoring case, else false.
Syntax:
str2.equalsIgnoreCase(str1);
Here str1 and str2 both are the strings which are to be compared.
Examples:
Input 1: GeeksforGeeks Input 2: Practice Output: false Input 1: Geeks Input 2: Geeks Output: true Input 1: geeks Input 2: Geeks Output: false
Program:
// Java program to Compare two strings// lexicographicallypublicclassGFG {publicstaticvoidmain(String args[]){String string1 =newString("Geeksforgeeks");String string2 =newString("Practice");String string3 =newString("Geeks");String string4 =newString("Geeks");String string5 =newString("geeks");// Comparing for String 1 != String 2System.out.println("Comparing "+ string1 +" and "+ string2+" : "+ string1.equalsIgnoreCase(string2));// Comparing for String 3 = String 4System.out.println("Comparing "+ string3 +" and "+ string4+" : "+ string3.equalsIgnoreCase(string4));// Comparing for String 4 = String 5System.out.println("Comparing "+ string4 +" and "+ string5+" : "+ string4.equalsIgnoreCase(string5));// Comparing for String 1 != String 4System.out.println("Comparing "+ string1 +" and "+ string4+" : "+ string1.equalsIgnoreCase(string4));}}chevron_rightfilter_noneOutput:Comparing Geeksforgeeks and Practice : false Comparing Geeks and Geeks : true Comparing Geeks and geeks : true Comparing Geeksforgeeks and Geeks : false
- Using Objects.equals() : Object.equals(Object a,Object b) method returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals() method of the first argument.
Syntax:
public static boolean equals(Object a, Object b)
Here a and b both are the string objects which are to be compared.
Examples:
Input 1: GeeksforGeeks Input 2: Practice Output: false Input 1: Geeks Input 2: Geeks Output: true Input 1: null Input 2: null Output: true
Program:
// Java program to Compare two strings// lexicographicallyimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){String string1 =newString("Geeksforgeeks");String string2 =newString("Geeks");String string3 =newString("Geeks");String string4 =null;String string5 =null;// Comparing for String 1 != String 2System.out.println("Comparing "+ string1 +" and "+ string2+" : "+ Objects.equals(string1, string2));// Comparing for String 2 = String 3System.out.println("Comparing "+ string2 +" and "+ string3+" : "+ Objects.equals(string2, string3));// Comparing for String 1 != String 4System.out.println("Comparing "+ string1 +" and "+ string4+" : "+ Objects.equals(string1, string4));// Comparing for String 4 = String 5System.out.println("Comparing "+ string4 +" and "+ string5+" : "+ Objects.equals(string4, string5));}}chevron_rightfilter_noneOutput:Comparing Geeksforgeeks and Geeks : false Comparing Geeks and Geeks : true Comparing Geeksforgeeks and null : false Comparing null and null : true
- Using String.compareTo() :
Syntax:
int str1.compareTo(String str2)
Working:
It compares and returns the following values as follows:- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
i.e.(string1 == string2) it returns 0. - if (string1 < string2) it returns a negative value.
Examples:
Input 1: GeeksforGeeks Input 2: Practice Output: -9 Input 1: Geeks Input 2: Geeks Output: 0 Input 1: GeeksforGeeks Input 2: Geeks Output: 8
Program:
// Java program to Compare two strings// lexicographicallyimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){String string1 =newString("Geeksforgeeks");String string2 =newString("Practice");String string3 =newString("Geeks");String string4 =newString("Geeks");// Comparing for String 1 < String 2System.out.println("Comparing "+ string1 +" and "+ string2+" : "+ string1.compareTo(string2));// Comparing for String 3 = String 4System.out.println("Comparing "+ string3 +" and "+ string4+" : "+ string3.compareTo(string4));// Comparing for String 1 > String 4System.out.println("Comparing "+ string1 +" and "+ string4+" : "+ string1.compareTo(string4));}}chevron_rightfilter_noneOutput:Comparing Geeksforgeeks and Practice : -9 Comparing Geeks and Geeks : 0 Comparing Geeksforgeeks and Geeks : 8
Why not to use == for comparison of Strings?
In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:
- Main difference between .equals() method and == operator is that one is method and other is operator.
- One can use == operators for reference comparison (address comparison) and .equals() method for content comparison.
- Both s1 and s2 refers to different objects.
- When one uses == operator for s1 and s2 comparison then the result is false as both have different addresses in memory.
- Using equals, the result is true because its only comparing the values given in s1 and s2.
- Compare two strings lexicographically in Java
- How to Initialize and Compare Strings in Java?
- Compare Dates in Java
- How to compare two arrays in Java?
- How compare() method works in Java
- Java Integer compare() method
- Short compare() method in Java
- Double compare() Method in Java with Examples
- Boolean compare() method in Java with Examples
- Byte compare() method in Java with examples
- Float compare() Method in Java with Examples
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
- Printing Integer between Strings in Java
- Numbers in Java (With 0 Prefix and with Strings)
- Output of Java Programs | Set 52 (Strings Class)
In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
Example:
// Java program to understand // why to avoid == operator public class Test { public static void main(String[] args) { String s1 = new String("HELLO"); String s2 = new String("HELLO"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } } |
false true
Explanation: Here String objects are being created namely s1 and s2.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



