Java String.equals vs ==



String.equals() compares the content while == checks whether the references are pointing to the same object or not.

Example

See the illustration example below −

public class Tester {
   public static void main(String[] args) {
      String test = new String("a");
      String test1 = new String("a");
      System.out.println(test == test1);
      System.out.println(test.equals(test1));
   }
}

Output

false
true
raja
Published on 06-Feb-2018 12:46:19
Advertisements