How to determine if two strings are equal in Java?

In Java, you can use the equals() method to determine if two String objects are equal. The equals() method compares the content of two strings to see if they are the same. For example:

String str1 = "Hello";
String str2 = "World";

if (str1.equals(str2)) {
    System.out.println("两个字符串相等");
} else {
    System.out.println("两个字符串不相等");
}

In the example above, the contents of str1 and str2 are not equal, so it will output “The two strings are not equal”.

Leave a Reply 0

Your email address will not be published. Required fields are marked *