Mystry of Java String.intern
Mystery of Java String.intern We have all learn about how Java has optimised handling string objects. The String class has been made immutable and string literal definition makes sure that existing instance from string pool will be returned instance creating new one. Here is simple code snippet that explains it String firstName = "John"; String firstNameWithNew = new String("John"); String duplicateFirstName = "John"; //All there strings are same System.out.println(firstName.equals(duplicateFirstName) && firstName.equals(firstNameWithNew));//=> true //Since they defiend using string iterals, even there memory references are same System.out.println(firstName == duplicateFirstName);//=> true //Since one of the object has defined explictly using new operator, there memory references are different. System.out.println(firstName == firstNameWithNew);//=> false This is very basic understanding that everyone has. Then I came across String.in