Posts

Exception Handling in Java

Image
  Exception Handling in Java : The  Exception Handling in Java  is one of the powerful  mechanism to handle the runtime errors  so that normal flow of the application can be maintained. In this page, we will learn about Java exceptions, its type and the difference between checked and unchecked exceptions. What is Exception in Java Dictionary Meaning:  Exception is an abnormal condition. What is Exception Handling Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Hierarchy of Java Exception classes Types of Java Exceptions There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions: Checked Exception Unchecked Exception Error Difference between Checked and Unchecked Exceptions 1) Checked Exception The classes which directly inherit Th

JAVA IMPORTANT QUESTIONS

EXAMPLE PROGRAM 1(APPLETS)

How to run an Applet? There are two ways to run an applet By html file. By appletViewer tool (for testing purpose). EXAMPLE PROGRAM : import java . applet .*; import java . awt .*; public class HelloWorldApplet extends Applet { public void paint ( Graphics g ) { g . drawString ( "Hello World" , 25 , 50 ); } } These import statements bring the classes into the scope of our applet class − import java.awt.applet.*; import java.awt.Graphics; Invoking an Applet <html> <title> The Hello, World Applet </title> <hr> <applet code = "HelloWorldApplet.class" width = "320" height = "120" > </applet> <hr> </html>

APPLET LIFE CYCLE

Image
Java Applet Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. Advantage of Applet There are many advantages of applet. They are as follows: It works at client side so less response time. Secured It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc. Drawback of Applet Plugin is required at client browser to execute applet. Lifecycle of Java Applet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed. Life Cycle of an Applet Four methods in the Applet class gives you the framework on which you build any serious applet − init  − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. start  − This method is automatically called after the brow

create thread in multi threading

How to create thread There are two ways to create a thread: By extending Thread class By implementing Runnable interface. Thread class: Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface. Runnable interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). public void run():  is used to perform action for a thread. Starting a thread: start() method  of Thread class is used to start a newly created thread. It performs following tasks: A new thread starts(with new callstack). The thread moves from New state to the Runnable state. When the thread gets a chance to execute, its target run() method will run. 1) Java Thread Example by extending Thread class class  Multi  extends  Thread{   public   void  run(){   System.out.print

Life cycle of a Thread

Image
Life cycle of a Thread A thread can be in one of the five states. According to sun, there is only 4 states in  thread life cycle in java  new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread in java is controlled by JVM. The java thread states are as follows: New Runnable Running Non-Runnable (Blocked) Terminated Following are the stages of the life cycle − 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 run

Java Thread Methods

Java Thread Methods S.N. Modifier and Type Method Description 1) void start() It is used to start the execution of the thread. 2) void run() It is used to do an action for a thread. 3) static void sleep() It sleeps a thread for the specified amount of time. 4) static Thread currentThread() It returns a reference to the currently executing thread object. 5) void join() It waits for a thread to die. 6) int getPriority() It returns the priority of the thread. 7) void setPriority() It changes the priority of the thread. 8) String getName() It returns the name of the thread. 9) void setName() It changes the name of the thread. 10) long getId() It returns the id of the thread. 11) boolean isAlive() It tests if the thread is alive.