Wednesday, August 15, 2018

Loop Statements in Java



Loop statement enables program to execute the code repeatedly until the condition satisfies.

There are 3 loop statement , that are

1) For loop
2) While loop
3) Do while loop

For loop :

The code in for loop will be executed from the start value until the condition is met.

Example :

public class Forloop
{
   public static void main(String[] args)
   {
      int i;
      for (i = 1; i <=10; i++)
         System.out.println("Testing Solutions");
   }
}

While loop :

The code in while loop will be executed at the time when the condition is true.

Example :

public class Whileloop
{
   public static void main(String[] args)
   {
      int i = 1;
      while (i <= 5)
      {
         System.out.println("Welcome to Testing Solutions");
         i++;
      }
   }
}

Do while loop :

The do while loop is like the while loop except that in do while loop, the block of code is at least executed one time although the condition is not true. The reason is that condition is checked at the bottom, not the top of the loop.

Example :

public class Dowhileloop
{
   public static void main(String[] args)
   {
      int i = 1;
      do
      {
         System.out.println("Testing Solutions");
         i++;
      } while (i <= 10);
   }
}

Thanks ,

A M Balaji

Conditional Statements in Java



The statement that programming language used to decide , which code has to be run when the condition is met.

There are 4 types of Conditional Statements ,  that are

1) if statement
2) if else statement
3) Nested if statement
4) Switch statement

if statement :

The if statement checks the condition and if the result is true , it runs the code.

Example :

public class Ifstatement
{
   public static void main(String[] args)
   {
      int i = 2;
      if (i%2 == 0)
         System.out.println("i is an even number");
   }
}

if else statement :

If the condition is true , then it will execute the "if statement" , else it will execute the "else statement".

Example :

public class Ifelsestatement
{
   public static void main(String[] args)
   {
      int i = 5;
      if (i%2==0)
         System.out.println(i+" is an even number.");
      else
         System.out.println(i+" is an odd number.");
   }
}

Nested if statement :

There will be many conditions in "nested if statement", where any condition can be satisfied.

Example :

public class Ifstatementnested
{
   public static void main(String[] args)
   {
      int i = 1;
      if (i > 0)
         System.out.println(i+" is Positive.");
      else
         if (i < 0)
            System.out.println (i+" is Negative.");
         else
            System.out.println (i+" is Zero.");
   }
}







 switch statement :



The switch statement takes a variable and then has a list of cases or actions to perform for each value that the variable obtains.It uses the word default to perform actions in which the conditions are not met and it uses the word break to stop performing actions.

Example :

public class Switchstatement
{
   public static void main(String[] args)
   {
      int i = 5;
      switch(i)
      {
            case 1:
                    System.out.println ("i is 1");
                    break;
            case 2:
                    System.out.println ("i is 2");
                    break;
            case 3:
                   System.out.println ("i is 3");
                    break;   
            case 4
                  System.out.println(“i is 4”);
                  break;
            case 5:
                  System.out.println(“i is 5”);
                  break;
            default:
                  System.out.println("Invalid value");
                  }
               }
            }

Thanks ,

A M Balaji

Monday, January 29, 2018

Try-Catch in Java



Java try block should be used inside the method and it is mainly used in code that might Throw Exception.

Note : Always Try block should be followed by either Catch block or Finally block.

Syntax of Try - Catch block and Try - Finally block is :

Try - Catch :

try {

        // Inside Code

} catch (Exception)
{

      // Catch Exception

}

Try - Finally :

try {

         //Inside Code
}finally (Exception)
{

     // Catch Exception

}

Java Catch block :

Catch block is used to Handle Exception which is thrown from the Try Block.

Here we can use many Catch block for a Try block.

What are all the problems we will get , while running the code with out Try - Catch block.

Code (Without Try-Catch):
  
public class Test{   
public static void main(String args[]){   
int data=50/0;//may throw exception   
System.out.println("rest of the code...");   
}
}  

Here the error will be "Exception in thread main java.lang.ArithmeticException:/ by zero".

But the Execution will stop at the same point , Where the error occurs . it will not continue execution and print the "rest of the code" line.

Code(With Try-Catch) :

public class Test{ 
public static void main(String args[]){ 
try{ 
int data=50/0; 
}catch(ArithmeticException e){System.out.println(e);} 
System.out.println("rest of the code..."); 

}


Here the error will be "Exception in thread main java.lang.ArithmeticException:/ by zero". 
With "rest of the code" line.







Thanks,

A M Balaji


   


Saturday, January 27, 2018

Java Data Types



Definition : Data type is the classification of the type of data , which can be used used in your programs as per the functionality .

Here java supports explicit declaration of data types , that is data type should be initialized before used as variable.

Syntax : data_type variable_name ;

data_type variable_name = value ;

Example :

int testing ;

int testing =100;

Here we have two types of data types ,

         1) Primitive Data Type
         2) Non primitive Data Type

Now we can see this in detail.

Primitive Data Type :

           1) Integer Types :

                     1)  byte - 8 bits(byte a = 100)
                     2)  short - 16 bits(short a = 100
                     3)  int - 32 bits(int a = 100)
                     4)  long - 64 bits(long a = 100000L)

           2) Rational Types :

                     1)  float - 32 bits(float a = 1.2)
                     2)  double - 64 bits(double a = 12.3456)

           3) Characters

                     1)  char -16 bits(char a = 'Z')

           4) Conditional

                     1) boolean - undefined (boolean a = true)

Non Primitive Data Type(Referenced Data Type):

             Objects and Arrays are the Non Primitive Data Types . And here Referenced
             Data Type are not "Passed by Value" and it is "Passed by Reference".

             Button b = new Button("OK")

Regards,

A M Balaji

Java Modifiers



Java Modifiers are used as keywords , which can be added to the definitions to change their meanings.

There are two types of modifiers which can be used in java .

       1) Access Modifiers
       2) Non Access Modifiers

First let us see about what are the Access Modifiers :

There are four types of access modifiers in java , which is used to control the classes, methods and variables .

       1) Private(private)
       2) Default(default)
       3) Protected(protected)
       4) Public(public)

Here we want to declare all the access modifiers in "Lowercase" letters as mentioned above in the bracket for all the modifiers.

Now lets as see the use of all the modifiers one by one .

private :

The private access modifier can be accessed only within the class

Example for the private modifier :

class test
{

private String testing = "testing";

}

default :

If there is no access modifier defined , then it will treated as default modifier.This can be accessible only with in the package .

Example for the default modifier :

class test
{

int testing =1;

}

protected :

The protected modifiers can is same as default modifier , can be accessed with in the package and other thing is , it can be accessed outside the package by using inheritance (Reuseability).

public :

This modifier can be accessible from everywhere.


Modifier
Within class
Within Package
Outside of the Package (by sub classes)
Outside of the Package
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y

Now let us see about Non access modifiers :

Here we have

       1) static
       2) final
       3) abstract

as Non access modifiers , which can be used in classes , methods and variables as per the functionality we have .

Thanks ,

A M Balaji