Friday, 12 September 2014

Constructor ,constructor interview questions

What is constructor?

Constructor is just like a method with same name of class that is used to initialize the state of an object. It is invoked at the time of object creation.

What is the purpose of default constructor?

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Does constructor return any value?

  yes, that is current instance

  Is constructor inherited?

   No, constructor is not inherited.

  

Can you make a constructor final?

  No, constructor can't be final.


Q. If we are using both constructor and Setter method of a bean to set property of a bean class then with which value it will be set , with value given in constructor or setter method ? 



Ans : Answer to this question in detail can be found here 
Read More »

Sunday, 7 September 2014

How to Create Threads in Java by Implementing Runnable Interface


                                                     


1.  Let your class implement “Runnable” interface.

2.  Now override the “public void run()” method and write your logic there (This is the method which will be executed when this thread is started).

That’s it, now you can start this thread as given below

1. Create an object of the above class

2.  Allocate a thread object for our thread

3.  Call the method “start” on the allocated thread object.

First Thread

public class FirstThread implements Runnable 
{   
  
  public void run()     
  {                 
    
    for ( int i=1; i<=10; i++)       
    {           
        //Displaying the numbers from this thread           
        System.out.println( "Messag from First Thread : " +i);                      
  
       /*taking a delay of one second before displaying next number               
        *             
        * "Thread.sleep(1000);" - when this statement is executed,            
        * this thread will sleep for 1000 milliseconds (1 second)            
        * before executing the next statement.            
        *             
        * Since we are making this thread to sleep for one second,            
        * we need to handle "InterruptedException". Our thread            
        * may throw this exception if it is interrupted while it              
        * is sleeping.            
        *             
        */         
        try            
        {               
           Thread.sleep (1000);             
        }           
        catch (InterruptedException interruptedException)           
        {               
           /*Interrupted exception will be thrown when a sleeping or waiting 
            *thread is interrupted.                   
            */             
            System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);           
        }   
    }   
  } 
}  

Second Thread


public class SecondThread implements Runnable 
{   
   //This method will be executed when this thread is executed  
   public void run()    
   {            
      //Looping from 1 to 10 to display numbers from 1 to 10        
      for ( int i=1; i<=10; i++)         
      {             
         System.out.println( "Messag from Second Thread : " +i);                        
         
        /*taking a delay of one second before displaying next number              
         *            
         * "Thread.sleep(1000);" - when this statement is executed,               
         * this thread will sleep for 1000 milliseconds (1 second)            
         * before executing the next statement.               
         *           
         * Since we are making this thread to sleep for one second,               
         * we need to handle "InterruptedException". Our thread               
         * may throw this exception if it is interrupted while it             
         * is sleeping.               
         */            
         try           
         {              
             Thread.sleep(1000);            
         }          
         catch (InterruptedException interruptedException)          
         {              
            /*Interrupted exception will be thrown when a sleeping or waiting                 
             * thread is interrupted.                 
             */                
             System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);             
         }      
      }     
    }  


MAIN CLASS

public class ThreadDemo
{
     public static void main(String args[])
     {
        //Creating an object of the first thread
        FirstThread   firstThread = new FirstThread();
      
        //Creating an object of the Second thread
        SecondThread   secondThread = new SecondThread();
      
        //Starting the first thread
        Thread thread1 = new Thread(firstThread);
        thread1.start();
      
        //Starting the second thread
        Thread thread2 = new Thread(secondThread);
        thread2.start();
     }
}


OUTPUT:

Output of ThreadDemo

Read More »

Saturday, 6 September 2014

Thread , Its Life Cycle and Multithreading

                                                             


My title
What is thread?

Thread is a sequence of code executed independently with other threads of control with   in a single executed program .

Java is a  multithreaded programming language which means we can develop multithreaded program using Java.

 A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.

Life Cycle of a Thread :

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.


Java Thread


Threads can be implemented in two ways:
1.By implementing runnable interface.
2. By extending thread class.


If we extend thread class then we need to override "run" method of thread class as it contains the logic to execute the thread.


After that we need to execute start() method.

Other methods supported by Threads are given below.
  • join(): It makes to wait for this thread to die. You can wait for a thread to finish by calling its join() method.
  • sleep(): It makes current executing thread to sleep for a specified interval of time. Time is in milli seconds.
  • yield(): It makes current executing thread object to pause temporarily and gives control to other thread to execute.
  • notify(): This method is inherited from Object class. This method wakes up a single thread that is waiting on this object's monitor to acquire lock.
  • notifyAll(): This method is inherited from Object class. This method wakes up all threads that are waiting on this object's monitor to acquire lock.
  • wait(): This method is inherited from Object class. This method makes current thread to wait until another thread invokes the notify() or the notifyAll() for this object.

JAVA THREAD PROGRAMS :
1. BY IMPLEMENTING RUNNABLE INTERFACE.

2. BY EXTENDING THREAD CLASS.
Read More »

Difference between String ,String Builder and String Buffer



                                                           


String :

 String is immutable object i.e. once created cannot be changed.
object created is stored in Constant Spring pool.
Immutable objects means they can't be modified.
They are thread safe means can be used by only single thread at a time.

e.g. String value1="test";

this object is stored in constant pool and value cannot be modified.

How to reverse a string in java

StringBuffer :


StringBuffer is mutuable i.e. values can be changed.

It is defined as

String value1= new String ("test");

These values are stored in heap and can be modified at later point  of time

i.e. value1=new String ("testing");

now value1 is referring to an object which has value "testing".

 String Buffer is also synchronized means thread-safe.

Difference between Application Server and Web server 
String Builder :

String builder is also as same as String Buffer .It also stores value in heap.But unlike StringBuffer , String Builder is non synchronized which makes it working faster.


You might like :

Custom Exception in Java -Tutorial
Life Cycle of a thread 
How to reverse a string in java
Read More »

How Hashmap works in Java

Read More »

Friday, 5 September 2014

Samsung Galaxy Note 4



                                                                



Samsung has launched the Galaxy Note 4 at its pre-IFA 2014 event, Samsung Unpacked 2014 Episode 2. Samsung also launched a unique smartphone with a side display called the Galaxy Note Edge.

The Samsung Galaxy Note 4 comes with the new S Pen stylus with improved pressure sensitivity, as well as the new Smart Select feature, apart from the new calligraphy and signing pen writing options.


The Samsung Galaxy Note 4 runs on Android 4.4 KitKat, and features a 5.7-inch Quad-HD (1440x2560 pixel) Super AMOLED display with a pixel density of 515ppi. It weighs in at 176 grams, and is 8.5mm-thick. 



The Galaxy Note 4 will be made available in 4G LTE and 4G LTE Cat.6 (LTE Advanced) variants depending on the region. The processor will be either a 2.7GHz quad-core processor, or a 1.9GHz octa-core (1.9GHz quad-core + 1.3GHz quad-Core) processor. Both feature 3GB of RAM.



The Samsung Galaxy Note 4 features a 16-megapixel autofocus rear camera with Smart OIS, and a 3.7-megapixel front-facing camera with a f/1.9 lens. It comes with 32GB of built-in storage, and expandability via microSD card. Connectivity options include Wi-Fi 802.11 a/ b/ g/ n/ ac, GPS/ Glonass, NFC, Bluetooth v 4., IR LED, USB 2.0, and MHL 3.0.



Sensors onboard the Galaxy Note 4 include an accelerometer, geo-magnetic, gyroscope, RGB, IR-LED, proximity, barometer, hall sensor, finger scanner, UV, heart rate monitor. It features a 3220mAh battery.

Samsung is also touting the Multi-Window feature available from the Recent button.
Read More »