Friday, February 13, 2015

Finally

Finally is a block of code which would be executed irrespective of the exception exist or not, generally it is known as the clean up code, it is used to close the database connection if open etc.

package Example1;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionAgain {
 static Scanner scanobj=new Scanner(System.in);
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("how old are you");
  int age=checktheage();
  if(age!=0){
   System.out.println(" you are "+age+" old");
  }
 }

 public static int checktheage(){
  try{
   return scanobj.nextInt();
  }
  catch(InputMismatchException e){
   System.out.println("Do not enter whole number");
   return 0;
  }
  finally{
   System.out.println("clean up code");
  }
 }
}


>>>
------------------------------------------------------------------------------------------------------------

Exception Handling

The below example catches exception if you enter a value other than int

package Example1;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionAgain {
 static Scanner scanobj=new Scanner(System.in);
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("how old are you");
  int age=checktheage();
  if(age!=0){
   System.out.println(" you are "+age+" old");
  }
 }

 public static int checktheage(){
  try{
   return scanobj.nextInt();
  }
  catch(InputMismatchException e){
   System.out.println("Do not enter whole number");
   return 0;
  }
 }
}

- We can add multiple catch block however the most appropriate exception should be mentioned first then the least excepted exception and then general exception.
- If we want to ignore an exception then catch the exception but do not do anything inside the block example is shown below

catch(InputMismatchException e){}
- Catching multiple exception in single catch is also possible.
catch(InputMismatchException | IOException e){} >>>Finally
------------------------------------------------------------------------------------------------------------

Exception

Exception highlights/handles the errors. Please find the example how to catch the exception and display so that we can take an appropriate action on the statement that is triggering this.


package Example1;

public class ExceptionExample {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  dividebyzero(2);
 }
 public static void dividebyzero(int a){
  try{
   a=a/0;
  }
  catch(Exception e){
   System.out.println(e.getMessage());
   System.out.println(e.toString());
   e.printStackTrace();
  }
 }
}

output:
/ by zero
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
at Example1.ExceptionExample.dividebyzero(ExceptionExample.java:11)
at Example1.ExceptionExample.main(ExceptionExample.java:7)

>>>Exceptions
------------------------------------------------------------------------------------------------------------

Random Number

Random Number Generation example

package Example1;
import java.util.Scanner;

public class RandomNumber {
 static int RandomNumber;
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("The Random Number generated is "+GenerateRandom());
  Scanner obj=new Scanner(System.in);
  
  
  int guessednumber=1;
  while(guessednumber!=-1){
   System.out.println("Guess a Number between 1 to 50?");
   guessednumber=obj.nextInt();
   guessednumber=CheckRandom(guessednumber);
  }
  System.out.println("yes you guessed it is "+RandomNumber);
 }
 
 public static int GenerateRandom(){  
  RandomNumber=(int)(Math.random()*51);
  return RandomNumber;
 }
 
 public static int CheckRandom(int passedguessednumber){ 
  if(passedguessednumber==RandomNumber){
   return -1;
  }
  else{
   return passedguessednumber;
  }
 }


output:
You need to execute in order to view the result >>>Exception
------------------------------------------------------------------------------------------------------------

Pass By Value

Pass by value is explained in the below example.

package Example1;

public class PassByValue {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
   int x=10;
   display(x);//value of 10 is passed
   System.out.println("x value in the main method "+x);
 }

 public static void display(int a){
  a=a+1;
  System.out.println("x value in the display "+a);
 }
}

output:
x value in the display 11
x value in the main method 10
>>>Random Number Generation
------------------------------------------------------------------------------------------------------------

Local Variable vs Class Variable

When a variable is defined in a class it is the gloabl variable and it can be accessed from any class or method. When a variable is defined inside a method then it is the local variable.Check the below example which explains the below concepts.

package Example1;

public class LocalVsGlobal {
 static int interest=10;//global variable
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  display();
  System.out.println("inside the main method "+interest);
 }
 public static void display(){
  int interest=8;// local variable
  System.out.println("Inside the display method "+interest);
 }
}

output:
Inside the display method 8
inside the main method 10

The value of interest inside display method uses the local variable value which is 8, while the interest value inside main method uses gloabl variable and that is the reason it is different.
If we want the gloabl variable value to be updated with the local variable value then just remove the keyword int it and it affects all value in all places. >>>Pass By value
------------------------------------------------------------------------------------------------------------

Do While

Do while is another type of looping and how is it different from while loop. It executes the do part irrespective of the while condition for the first time,you can check the below example.

package Example1;

public class DoWhileExample {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int i=10;
  do{
   System.out.println(i);
  }while(i>10);
 }

}


output:
10

In the above example the do block is executed irrespective of whether the while condition is true or false. if the same scenario was used for while loops then nothing is printed in the console reason the while condition is false. >>>Local Variable vs Class Variable
------------------------------------------------------------------------------------------------------------

For Loop

A simple For loop example which illustrates 6 multiples and this can be extended for any multiples.


package Example1;

public class ForLoopExample {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("Display's 6 multiples");
  for(int i=6;i<=60;i+=6){
   if(i%6==0){
    System.out.println(i);
   }
  }
 }

}


output:
Display's 6 multiples
6
12
18
24
30
36
42
48
54
60

>>>do While
------------------------------------------------------------------------------------------------------------

While Loop With Break and Continue

This section exlains about While loop with break and continue, the below example explains how the flow is affected with the usage of break/continue.


package Example1;

public class WhileLoopWithBreak {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int j=0;
  while(j<=10){
   System.out.println(j);
   j++;
   if(j==5){
    System.out.println("its five");
    if(j%5==0){
     break;
    }    
   }
  }
 }
}

output:
0
1
2
3
4
its five



package Example1;

public class WhileLoopWithContinue {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int j=0;
  while(j<=10){
   System.out.println(j);
   j++;
   if(j==5){
    System.out.println("its five");
    if(j%5==0){
     continue;
    }    
   }
  }
 }
}

output:
0
1
2
3
4
its five
5
6
7
8
9
10

When break is used it total comes out of all the loops while continue break the existing loop and continue from the beginning of the loop. >>>ForLoop
------------------------------------------------------------------------------------------------------------