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

Updated on: 24-Feb-2020

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements