When you create string literals jvm maintain all of them in pool known as String pooling.
for exapmle,
String s1 = "Arul"; //case 1
String s2 = "Arul"; //case 2
In case 1, iteral s1 is created newly and kept in the pool. But in case 2, literal s2 refer the s1, it will not create new one instead.
if(s1 == s2) System.out.println("equal"); //Prints equal.
String n1 = new String("Arul");
String n2 = new String("Arul");
if(n1 == n2) System.out.println("equlal"); //No output.
Arulmurugan.S
|