Last updated on November 18th, 2015 at 03:41 am

Please find below a set of beginners java tutorial.
EXCEPTION HANDLING IN JAVA
Run time errors are called as exceptions. Java provides a way to deal with these run time errors. A run time error can occur due to illegal operation such as an integer divided by zero. Java has a class called throw able that deals with exceptions and errors. This class has a subclass called exception. The exception class has further subclasses such as arithmetic exception, I/O exception, ArrayIndexOutOfBOund exception etc. there are two reserved words in java for dealing with exception, which are try and catch. We will look at some example that how we deal with exceptions.
Class myclass
{
public static void main(String arg[])
{
int a, b, c;
try
{
a=20;
b=0;
c=a/b;
}
catch(ArithmeticException e)
{
System.out.println(“:zero divide error”);
}
}
}

in the above program the try clause tells the compiler to try to execute the code. But if it fails to execute then an exception is generated and is thrown which is caught by catch clause. In java there maybe multiple catches to catch an exception. The following is an example of it.
Class test
{
public static void main(String arg[])
{
int a,b,n[];
try
{
n=new int[2];
b=arg.length;
a=20/b;
n[2]=a;
System.out.println(“RESULT”+a);
}
catch(ArithmeticException a)
{
System.out.println(“zero divide error”);
}
catch(ArrayIndexOutofBoundException e)
{
System.out.println(“error”+e);
}
}
}

similarly there maybe multiple try clauses in java.
THROW AND THROWS
So far java runtime system was throwing exceptions. Your program can also throw exceptions explicitly using a throw statement. The general form of throw is as:Throw throwable instance
Here throwable object must be an object of type throwable or a subclass of throwable. Here we see an example.
Class test
{
public static void ExcepTest() throws Arithmetic Exception
{
throw new ArithmeticExcption(“demo”);
}
public static void main(String arg[])
{
try
{
ExcepTest();
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}

in java if a method is capable of throwing an exception then it must specify its this behavior so that callers of that method can guard themselves against this exception. This can be done by using throws statement in declaration of that method. We will see a exception program to understand this clearly.

class Myexception extends Exception
{
protected int errno;
Myexception(int n)
{
errno=n;
}
public String toString()
{
return “Myexception:”+errno;
}
}
class test
{
public static void ExcepTest(int a) throws Myexception
{
if(a>1&& a<=10)
System.out.println(“number:”+a);
Else
Throw new Myexception(a);
}
public static void main(String arg[])
{
try
{
ExcepTest(5);
ExcepTest(10);
ExcepTEst(15);
}
catch(Myexception c)
{
System.out.println(“invalid number:”+e)
}
}
}

FINALLY CLAUSE
As exception is caught, the program terminates and you see the output, what ever it is. If in a program there is a chance of exception then you will put that code in try/catch clause. Now in same the program if you have some code which you want to be executed in every condition (whether before exception or after it is caught), then you will put that code in finally clause. The code in this clause executes in every condition even if the exception is generated and is caught. Following is the program.

Class myclass
{
public static void main(String arg[])
{
int a=3, b=0,c;
try
{
c=a/b;
}
catch(ArithmeticException e)
{
System.out.println(“zero divide error”);
}
finally
{
System.out.println(“checking of FINALLY clause “);
}

}
}

MULTITHREADING IN JAVA
Executable part of an application is called as thread. We often interact with threads while using many applications. For example, if we use MS-word application then we see that a thread (spell checker) runs continuously as we continue typing. It continuously checks the spelling of words we type, from its dictionary. Java provides a way to create these threads. There are two ways for creating threads in java. One is by extending THREAD class and other is by implementing EUNNABLE interface. We will see an example of creating thread. As we know that the main program is itself a thread so therefore we will see a sample program of main program.
Class test
{
public static void main(String arg[])
{
Thread t=Thread.currentThread();
System.out.println(t);
t.setName(“test thread”);
System.out.println(t)
}
}

the output of this program will give the thread’s name that is “main”, its priority that is 5 and its representation. The priorities of thread are between 1 and 10 the normal priority is
5. Now we will see how we create threads from extending thread class.

Class newthread extends Thread
{
newthread()
{
super(“demo thread”);
System.out.println(“child thread:”+this);
Start();
}
public void run()
{
try
{
for(int i=5;i>0;i–)
{
System.out.println(“child thread:”=i);
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(“child interrupted”);
}
System.out.println(“exiting child thread”);
}
}
class demo
{
public static void main(String arg[])
{
new Newthread();
try
{
for(int i=5;i>0i–)
{
System.out.println(“main thread:”+i);
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(“main thread interrupted”)
}
System.out.println(“main thread exiting”);
}
}

in java threads can be prioritized. They can be given priorities between 1 and 10. there are several methods for thread for example isAlive() and join(). There is possibility that one thread can communicate with another. For example one thread may want to know whether another thread is alive or dead. This is done by isAlive() method. If you want to know that a thread is finished or not, you can call isAlive() method defined by thread class. Its general form is:
final Boolean isAlive()

if it returns true then it means thread is running. If it returns false then thread has stooped. While isAlive() is occasionally useful, the method that you will commonly use to wait for a thread to finish is called join().
Final void join()throws InterruptedExcepption

This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread until the specified thread joins it. Additional forms of join() allow you to specify a maximum amount of time that you want to wait for specified thread to terminate.

SYNCHRONIZATION
When two or more threads want to share some shared resources then it must be ensured that only one thread will use that resource at a time. The process by which it is done is called synchronization. We will see an example of it.
class callme
{
void call(String msg)
{
System.out.println(“[“+msg);
Try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(“interrupted”);
}
System.out.println(“]”);
}
}
class caller implements Runnable
{
String msg;
Callme target;
Thread t;
Public Caller(Callme targ, String s)
{
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run()
{
target.call9msg);
}
}
class Synch
{
public static void main(String arg[])
{
Callme target=new Callme();
Caller ob1=new Caller(target,”hello”);
Caller ob2=new Caller(target,”Synchronized”);
Caller ob3=new Caller(target,”world”);
Try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println(“interrupted”);
}
}
}

Conclusion
As Java’s popularity grows, it becomes more and more crucial to provide reliable ways to write Java programs that perform scientific computation. There are basically two reasons why Java is often claimed to be inappropriate for scientific computing. The first reason is that Java is generally interpreted and is therefore unable to run at machine speed. The second reason is that no standard numerical libraries have been translated yet to Java bytecode (or source for that matter). The first reason seems to be less and less or a concern as native compilers are being provided by software developers, so that Java can be used as any other language to develop code on those platforms. Even the speed of applets can be raised to new levels with JIT techniques. However, compiled Java source on those platforms is of course not portable anymore as it is not targeting the JVM any more.

Leave a Reply

Your email address will not be published. Required fields are marked *