A string in java is immutable; after its creation, it cannot be changed. also, any string literal will be stored in the string pool for later use if the same literal is used again, for example:
String name = "my name"; // one intern object is created in pool String otherName = "my name"; // the old intern object is reused System.out.println( name == otherName); // true , the same reference refer to same object
two links refer to the same location in the pool.
String name = new String("my name"); // one object is created, no string pool checking String otherName = new String("my name"); // other object is created, no string pool checking System.out.println( name == otherName); // false, we have 2 different object
here we have two different String objects in memory, each of which has its own meaning.
see this article: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
source share