Difference between String literal and New String object in Java

PHOTO EMBED

Mon Sep 07 2020 03:49:21 GMT+0000 (Coordinated Universal Time)

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
content_copyCOPY

https://www.java67.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html