Check string for equality : String equal « Data Type « Java






Check string for equality

 
 
public class Main {
  public static void main(String[] args) {
    String s1 = "Hello World";
    String s2 = new String("Hello World");

    if (s1.equals(s2)) {
      System.out.println("String is equal");
    } else {
      System.out.println("String is unequal");
    }
    if (s1 == s2) {
      System.out.println("String is equal");
    } else {
      System.out.println("String is unequal");
    }
  }
}

   
  








Related examples in the same category

1.String: equalString: equal
2.Determine whether a string is a palindrome?