how to test if a string equals another string 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: string check if substring exists java

String str1 = "Java";
String str2 = "va";

if(str1.contains(str2)){
  // str1.contains(str2) will be true here.
  System.out.println("I am true");
}

Tags:

Java Example