pre-knowledge

description

In short, when you create a String object, you DO create new places in memory (See image below; object 1, 2 and 3 have different memory locations)

However, every time you create a new String literal you don’t create a new String literal, but you reference to the same memory location. (See image below).

String literal vs object

CODE

the image above translated to code;

package String;

public class LiteralvsObject {
	
	String literal1 = "";
	String literal2 = "";
	String literal3 = "";
	
	String object1 = new String("");
	String object2 = new String("");
	String object3 = new String("");
	
}