Difference between String literal and New String object in Java


Mon Sep 07 2020 03:49:21 GMT+0000 (UTC)

Saved by @vrzi #java

String a = "Java";
String b = "Java";
System.out.println(a == b);  // True
// When you declare string like this, you are actually calling intern() method on String. 
// This method references internal pool of string objects.
// If there already exists a string value “Java”, 
// then str will reference of that string and no new String object will be created

String c = new String("Java");
String d = new String("Java");
System.out.println(c == d);  // False