Java equal string

‮‬https://www.lautturi.com
Java equal string

In Java, you can use the equals() method to compare the contents of two String objects for equality. The equals() method is defined in the Object class and is overridden by the String class to compare the contents of two String objects rather than their references.

Here's an example of how to use the equals() method to compare two String objects in Java:

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = new String("Hello");

        System.out.println(str1.equals(str2));  // true
        System.out.println(str1.equals(str3));  // true
    }
}

In this example, three String objects are created: str1, str2, and str3. str1 and str2 are initialized with the same string literal, and str3 is initialized with a new String object created from the same string literal.

The equals() method is called on str1 and passed str2 and str3 as arguments. The equals() method compares the contents of the String objects and returns true if they are equal, and false otherwise.

In this example, the equals() method returns true for both comparisons because the contents of the String objects are equal.

You can use the equals() method to compare the contents of two String objects in Java. Just call the equals() method on one of the String objects and pass the other String object as an argument.

Created Time:2017-11-03 15:57:10  Author:lautturi