Java – equals vs ==

String method equals check if both references points objects with same values stored in them. Operator == check if both references points to same object.

public class Main {
  public static void main(String[] args) {
    String str1 = "Hello";
    String str2 = new String("Hello");
    System.out.println(str1.equals(str2));
    System.out.println(str1 == str2);
  }
}
OpenJDK 64-Bit Server VM warning: Insufficient space for shared memory file:
   7869
Try using the -Djava.io.tmpdir= option to select an alternate temp location.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments