Posts

Showing posts from April, 2017

Exception in java

Image
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Exceptions are two types , one is runtime exception (occurs at runtime) and another one is compile time (occurs in compile time) .                                Runtime exceptions are  as follows :   DivideByZeroException   NullPointerException   ArithmeticException   ArrayIndexOutOfBoundsException    NumberFormatException etc Compile time exception is     Syntax error  Example of NullPointerException : public class HelloWorld {   public static void main(String[] args)   {     String str = null ;     System.out.println(" String length : "+ str.length() );   } }   Result:               Exception in thread "main" java.lang.NullPointerException               at HelloWorld.main(HelloWorld.java: 6 ) Example of   DivideByZeroException :  public class HelloWorld   {     public static void main(String[] args)     

Multithreading in java

Image
When more than one task is executing concurrently within a single program is called multithreading in Java . Thread in Java is controlled by java.lang.Thread class. Each program can be divided into number of small process . Each small process is called a single thread . Thread is a lightweight process. Thread can be created using this two way . implementing Runnable interface  extending Thread class    Advantage of Thread :         Threads are independent         Each task of a program can be executed and shared separated memory area.         It's a lightweight process .        Communication coast is low between the Threads .   Life cycle of a thread :                                    Example using Runnable interface : public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a runnable thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable(