Java String equals vs == Operator



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: 2020-02-24T10:21:12+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements