Singleton Design Pattern with Example and Program Code : Design pattern Interview Question

In this post we are going to learn about the singleton design pattern . It is the most used design pattern in web applications . So if you write design pattern in your CV , then make sure you are familiarized with Singleton
Pattern . For experienced people ,the most common question asked by the interviewer is to write down the code of the Singleton Pattern . Let us discuss that first .

Points to remember while creating a singleton class

Suppose we are creating a singleton class JavaHungry . It means only one  and only one instance of JavaHungry class we  can have at any point of execution in the application code  

*   We  should create a static variable so that it can hold one single instance of our class .
  
         private static JavaHungrySingleton  uniqueInstance ;

*   We should declare the constructor private so that  only Class itself can instantiate the object . Outside
     classes will not be able to see  the class constructor .

private   JavaHungrySingleton(){}

*  Then the main work  to write the method which returns the instance of the class , let the name of the    
    method be  getInstance() .

   Here in this method implementation we created the class JavaHungrySingleton .

  The return type should be of the class object . But there is possibility that two threads simultaneously try to
  access the method and we might end up with two different class objects , thats why method is     synchronized . In other words , By adding synchronized keyword to the method , we force every thread
  to wait for its turn before it can enter the method .  So that , two threads do not enter the method at the    
  same time .
   So we have set up the framework for the singleton method ,which  upto now  looks like

public static synchronized  JavaHungrySingleton   getInstance()
{
    1.               // some code
}

    Now we need to write  code for the method , that is line 1 //some code .

    So to achieve uniqueinstance , first and foremost important thing to check whether the object of the class
    is already in existence or not . We make sure , by putting condition

Pseudo code to achieve singleton 

   if  uniqueinstance is NULL
   

Difference Between String , StringBuilder and StringBuffer Classes with Example : Java

Today we are going to understand the difference between String , StringBuilder and StringBuffer . As you will find that there are minor differences between the above mentioned classes.

String

String is immutable  ( once created can not be changed )object  . The object created as a String is stored in the  Constant String Pool  .
Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously.
String  once assigned can not be changed.

String  demo = " hello " ;
// The above object is stored in constant string pool and its value can not be modified.


demo="Bye" ;     //new "Bye" string is created in constant pool and referenced by the demo variable           
 // "hello" string still exists in string constant pool and its value is not overrided but we lost reference to the  "hello"string 

StringBuffer

StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap . StringBuffer  has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe .

Due to this it does not allow  two threads to simultaneously access the same method . Each method can be accessed by one thread at a time .

But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus  StringBuilder is faster than the StringBuffer when calling the same methods of each class.

StringBuffer value can be changed , it means it can be assigned to the new value . Nowadays its a most common interview question ,the differences between the above classes .
String Buffer can be converted to the string by using