Posts

Showing posts with the label design

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...

Serializable Singleton Class

Previously I have posted about Singleton Design Pattern implementation and handling concurrency . In this post I am going to explains challenges for serializable Singleton class. Lets know about Singleton Class ( Single Design Pattern), A Singleton class restricts access to its constructor to ensure that only a single instance is ever created. I have taken example from previous post only   1: /** 2: * Singleton Class 3: * 4: * @author ydevatra 5: * 6: */ 7: public class SingletonResource implements Serializable { 8:   9: /* Private instance reference */ 10: private static SingletonResource resource = new SingletonResource(); 11:   12: /** 13: * Private constructor to restrict creation of instance of this class by 14: * other class. 15: */ 16: private SingletonResource() { 17: // Initialization code... 18: } 19:   20: ...