check 2 strings are equal in java code example

Example 1: java test if strings are equal

//This expression is true if str1 and str2 are equal, and false if they're not
str1.equals(str2)

//example use case:
String location = "London";
String destination = "London";
if (location.equals(destination)) {
	System.out.println("You have arrived!");
}

Example 2: how to know if String is the same java

String1.equals(String2)

Example 3: compare two strings java

String string1 = "using equals method";String string2 = "using equals method";        String string3 = "using EQUALS method";String string4 = new String("using equals method"); assertThat(string1.equals(string2)).isTrue();assertThat(string1.equals(string4)).isTrue(); assertThat(string1.equals(null)).isFalse();assertThat(string1.equals(string3)).isFalse();

Tags:

Java Example