Java Program to Compare Strings

Java Program to Compare Strings

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to check if two strings are anagram.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

String Comparison :

In Java, strings can be compared using the “==” operator, the “!=” operator, the “>” and “<” operator, and also we can use the compareTo() method of String Class.

Examples:

Using the “==” operator:

Two strings can be compared for equality using the “==” operator. For example, the following code will return “true” :

String string1 = "hello";
String string2 = "hello";
System.out.println(string1 == string2);

Using the “!=” operator:

Two strings can be compared for inequality using the “!=” operator. For example, the following code will return “false“:

String string1 = "hello";
String string2 = "world";
System.out.println(string1 != string2);

Using the .equals() method:

The .equals() method can be used to compare the value of two strings. It returns a boolean value of true or false. This method is recommended for string comparison in Java.

String string1 = "hello";
String string2 = "world";
System.out.println(string1.equals(string2));

Using the compareTo() method:

The compareTo() method can be used to compare two strings lexicographically. The method returns 0 if the string is equal to the other string, it returns a negative number if the string is less than the other string and a positive number if the string is greater than the other string.

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initialization of 2 Strings
        String string1 = "Lakshit";
        String string2 = "Mittal";
        
        // Comparing the strings using compareto function
        int comparisonResult = string1.compareTo(string2);
        
        // checking the returned value of compareto function
        if(comparisonResult == 0) {
            System.out.println("The strings are equal.");
        }else {
            System.out.println("The strings are not equal.");
        }
    }
}
Output:

The strings are not equal.

In this program, we are defining two strings, “Lakshit” and “Mittal”. We then use the compareTo() method on the first string, passing in the second string as the argument. This returns an integer, which we store in the variable comparisonResult.
We then use an if-else statement to check the value of comparisonResult. If it’s equal to 0, that means the strings are equal. If it’s not equal to 0, that means the strings are not equal.
This is a simple example but you can use similar logic to compare any number of strings in a program.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription