Java program to compare strings

https:‮‬//www.theitroad.com

In Java, you can compare two strings using the equals() method. Here's an example program that demonstrates how to compare two strings:

public class StringComparisonExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";
        String str3 = "Hello";
        
        if (str1.equals(str2)) {
            System.out.println("str1 is equal to str2");
        } else {
            System.out.println("str1 is not equal to str2");
        }
        
        if (str1.equals(str3)) {
            System.out.println("str1 is equal to str3");
        } else {
            System.out.println("str1 is not equal to str3");
        }
    }
}

In this program, three strings are created: str1, str2, and str3. The equals() method is then called on str1 with the argument str2, and on str1 with the argument str3.

The program then checks the results of these comparisons with an if statement, and prints out a message indicating whether the two strings are equal or not.

When you run the program, the output should be:

str1 is not equal to str2
str1 is equal to str3

As you can see, the equals() method correctly determines whether two strings are equal or not.