compare two strings in java
Code snippet on how to compare two strings in java
String str1 = "Hello";
String str2 = "Hello";
// Comparing the two strings
if(str1.equals(str2)) {
System.out.println("Strings are equal.");
}
This code is comparing the two strings for equality. It starts by declaring two String variables, “str1” and “str2”, and assigning the same string “Hello” to both of them. The code then uses the equals method to compare them. This method will compare the contents of the two strings, rather than the objects themselves as == would do. If the strings are equal, the code prints out “Strings are equal”. If they are not equal, no output is generated.