Showing posts with label java tutorial. Show all posts
Showing posts with label java tutorial. Show all posts

Monday, 9 February 2015

Multithreading in Java


What is Multithreading?
Life Cycle of a Thread
thread vs proccess.
Thread Scheduler
Creating Thread
Sleeping a thread
Start a thread twice
Calling run() method
Joining a thread
Naming a thread
Thread Priority
Daemon Thread
Thread Pool
Synchronization
Messaging
Thread Class
Runnable Interface .
IsAlive()
Join()

Sunday, 8 February 2015

Try And Finally Without Catch Block

1. We Can Use Finally after try block ,it will run gives message in finally block.But can not handle exception.
Syntax:

try{


}Finally{


}

public class ExceptionSample {
public static void main(String[] args) {
try {
int i=10/0;
}finally{
System.out.println("Finally Block");
}
System.out.println("You Are Come Back In Main");
}

}

Output:
Exception in thread "main" Finally Block
java.lang.ArithmeticException: / by zero
at exception_handling.ExceptionSample.main(ExceptionSample.java:6)

Finally Block In Java

Finally block always execute after try catch block.

1.
 package exception_handling;

public class FinallyBlock {
           
public static void main(String[] args) {
try {
int i=10/0;
} catch (Exception e) {
System.out.println(e);
}
finally{
System.out.println("You Are In Finally Block");
}
}
}


Output:
java.lang.ArithmeticException: / by zero
You Are In Finally Block

2.

package exception_handling;

public class FinallyBlock {
            
public static void main(String[] args) {
try {
int i=10/10;
} catch (Exception e) {
System.out.println(e);
}
finally{
System.out.println("You Are In Finally Block");
}
}
}

Output:
You Are In Finally Block

3. If You Use Return Statement in try /catch block then finally always execute  

package exception_handling;

public class FinallyReturn {
int display(){
try {
System.out.println("In display try"); 
return 2;
} catch (Exception e) {
System.out.println(e);
}finally{
System.out.println("Hi Finally Here");
}
return 1;
}
public static void main(String[] args) {
FinallyReturn finallyReturn=new FinallyReturn();
finallyReturn.display();
}
}

Output:
In display try
Hi Finally Here


4. If we use exit(0) in  try block then finally block will not execute.
package exception_handling;

public class FinallyExit {

public static void main(String[] args) {
try {
System.out.println("welcome Dear");
System.exit(0);
} catch (Exception e) {
System.out.println(e);
}finally{
System.out.println("Welcome In Finally Block");
}
}
}

Output:
welcome Dear

Wednesday, 21 January 2015

Java Tutorial

 

Java Tutorial


OOP Concept:




      Programs :



Java Collections Framework Tutorials For Beginners:





Data Structure in Java