
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java String Comparison Sample Code Examples
In this article, we will learn how to compare strings in Java using the compareTo() method and the == operator. These methods help in checking string values and references.
comapareTo() method
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.
- The == operator: You can compare two strings using the == operator. However, it compares references to the given variables, not values.
Steps for string comparison
We can compare Strings in Java using the compareTo() method and the == operator. The following are the steps for string comparison ?
-
Step 1: Declare and initialize strings: We declare two string variables str1 and str2 with the values "tutorials" and "point" respectively.
String str1 = "tutorials", str2 = "point";
-
Step 2: Compare string references using ==: We use the == operator to check if str1 and str2 refer to the same memory location (i.e., reference comparison).
System.out.println(str1 == str2);
-
Step 3: Compare string values using compareTo(): The compareTo() method is used to compare the actual values of str1 and str2 lexicographically. The result is stored in the variable.
int retval = str1.compareTo(str2);
-
Step 4: Return the comparison result: We use an if-else block to check the value of retval and print whether str1 is greater than, equal to, or less than str2.
if (retval < 0) {
System.out.println("str1 is greater than str2");
} else if (retval == 0) {
System.out.println("str1 is equal to str2");
} else {
System.out.println("str1 is less than str2");
}
Java program for string comparison using the compareTo() method
The following is an example of comparing a string with the compareTo() method ?
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "tutorials", str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); System.out.println(str1==str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } }
Output
false str1 is less than str2
Comparing strings using the equals() method
The equals() method in the String class is a straightforward way to compare two strings in Java. This function helps check whether the contents of two strings are the same.
Syntax:
public boolean equals(Object anotherObject)
-
Step 1. Declare two string variables: Two string variables str1 and str2 are created with the value "Hello".
String str1 = "Hello";
String str2 = "Hello";
-
Step 2. Compare strings using the equals() method: The equals() method compares str1 and str2. If they are equal, a message is printed saying the strings are equal; otherwise, it prints they are not.
if (str1.equals(str2)) {
System.out.println("str1 and str2 are equal.");
} else {
System.out.println("str1 and str2 are not equal.");
}
Example
public class StringComparisonExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; // Comparing strings using equals() method if (str1.equals(str2)) { System.out.println("str1 and str2 are equal."); } else { System.out.println("str1 and str2 are not equal."); } } }
Output
str1 and str2 are equal.