Stringpool.java
public class Stringpool {//definition
public static void Main (String args[])//main is common, static, no return value
{
String s0= "Hello";//defines a string
String s1= "Hello";//defines a string
String s2= "He" + "llo";//define a string
System.out.println (S0==S1);//true//output
System.out.println (S0==S2);//true//output
System.out.println (New string ("Hello") ==new string ("Hello"));//false//output
}
}
In Java, a string constant with the same content ("Hello") holds only one copy to conserve memory, so s0,s1,s2 actually refers to the same object. When the compiler compiles a s2 sentence, it strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization work is done automatically by the Java compiler. When you create a string object directly using the New keyword, the value is the same (all "Hello"), but it is still two separate objects.
Hands on the brain 20151024