Java compare two strings
In this post, you will learn how to compare two strings using the Java programming language.
A Java string is a sequence of characters that exists as an object of the class java.lang. In Java, a string is an immutable object, which means it is constant and cannot be changed once it has been created.
There are different ways to compare strings in Java. Here, we have mentioned most of them-
Comparing strings in Java using equals() Method
The equals() method of the Java String class compares the two given strings based on the content of the string. It returns a boolean value. If all characters are matched, it returns true; otherwise, it returns false. In the given program, we have used the equals() method for comparing strings.
// Java program to compare strings
public class StringComparison {
public static void main(String args[]){
String str1="etutorialspoint";
String str2="Etutorialspoint";
String str3=new String("etutorialspoint");
String str4="etp";
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true
System.out.println(str1.equals(str4));//false
System.out.println(str2.equals(str3));//false
}
}
Output of the above code:
false
true
false
false
The above program compares strings without ignoring cases. The String class provides equalsIgnoreCase() method for comparing strings, ignoring case.
// Java program to compare strings
public class StringComparison {
public static void main(String args[]){
String str1="etutorialspoint";
String str2="Etutorialspoint";
String str3=new String("etutorialspoint");
String str4="etp";
System.out.println(str1.equalsIgnoreCase(str2));
System.out.println(str1.equalsIgnoreCase(str3));
System.out.println(str1.equalsIgnoreCase(str4));
System.out.println(str2.equalsIgnoreCase(str3));
}
}
Output of the above code:
true
true
false
true
Comparing strings in Java using == operator
The "==" operator in Java is used to compare objects to check equality. This operator is used to compare both primitives and objects. It checks the reference equality of the two strings, whether they are pointing towards the same string object.
// Java program to compare strings
public class StringComparison {
public static void main(String args[]){
String str1="etutorialspoint";
String str2="etutorialspoint";
String str3=new String("eTutorialspoint");
String str4="etp";
System.out.println(str1==str2);
System.out.println(str1==str3);
System.out.println(str1==str4);
System.out.println(str2==str3);
}
}
Output of the above code:
true
false
false
false
Comparing strings in Java using compareTo() method
The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, a negative number, or zero, i.e., the number difference between two strings. If the string is equal to the other string it returns 0, if the string has fewer characters than the other string. It returns less than 0, and if the string has more characters than the other string, it returns greater than 0. It compares strings on the basis of the Unicode value of each character in the strings.
// Java program to compare strings
public class StringComparison {
public static void main(String args[]){
String str1="etutorialspoint";
String str2="etutorialspoint";
String str3=new String("eTutorialspoint");
String str4="etp";
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareTo(str3));
System.out.println(str1.compareTo(str4));
System.out.println(str2.compareTo(str3));
}
}
Output of the above code:
0
32
5
32
We can use the compareToIgnoreCase() method to compare strings ignoring case.
// Java program to compare strings
public class StringComparison {
public static void main(String args[]){
String str1="etutorialspoint";
String str2="etutorialspoint";
String str3=new String("eTutorialspoint");
String str4="etp";
System.out.println(str1.compareToIgnoreCase(str2));
System.out.println(str1.compareToIgnoreCase(str3));
System.out.println(str1.compareToIgnoreCase(str4));
System.out.println(str2.compareToIgnoreCase(str3));
}
}
Output of the above code:
0
0
5
0
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java