Blog

  • Java – Multi-Dimensional Array

    Declaring an array of arrays is known as Multidimensional Array or Jagged Arrays. In simple words, it is an array of arrays. It is created by appending a square brackets “[]” for each dimension. Here data are stored in a tabular form that is in row-major order as shown in an example below.

    Syntax:

     data-type name[size1][size2]...[sizeN];
     or
     dataType[size1][size2]...[sizeN] arrayRefVar;  
     or
     dataType [size1][size2]...[sizeN]arrayRefVar;  

    Declaration:

     int twodim[5][10][4];
     int threedim[5][10]; //data-type name[size1][size2]...[sizeN];

    Instantiate Multidimensional Array in Java:

     int[][] arr=new int[3][3];//3 row and 3 column

    Array initialization:

      arr[0][0]=1;  
      arr[0][1]=2;  
      arr[0][2]=3;  
      arr[1][0]=4;  
      arr[1][1]=5;  
      arr[1][2]=6;  
      arr[2][0]=7;  
      arr[2][1]=8;  
      arr[2][2]=9; 

    We can also declare and initialize the 2D array together:

     int arr[][]={ {1,2,3},{4,5,6},{7,8,9} };

    Example of a Multidimensional array in Java:

    public class MultiDimensionalTest 
    { 
      public static void main(String args[]) 
      { 
        // declaring and initializing 2D array 
        int array[][] = { {1,2,3},{4,5,6},{7,8,9} }; 
      
        // Diplaying 2D Array
        for (int i=0; i< 3 ; i++) 
        { 
          for (int j=0; j < 3 ; j++) 
             System.out.print(array[i][j] + " "); 
      
          System.out.println(""); 
        } 
      } 
    } 

    The output of Multi-dimensional:

    1 2 3 
    4 5 6 
    7 8 9 

  • Java – One-Dimensional Array with Example

    A one-dimensional array in Java is an array with a bunch of values that are declared with a single index. Here data are stored in a single column as shown below in an example.

    Array declaration:

     data_type [] array_Name;
     or
     data_type[] array-name 
     or
     data_type array-name[]; 

    Example:

    //to store integer value
     int intArray[];
     or
     int []intArray;

    An array declaration has two components:
    The type that is the data-type of an element and the name that is given by user choice with the brackets[].

    Array initialization:

    To initialize an array new is used and it appears as follows:

    var-name = new data-type [size];

    Example:

    Array = new int[10];

    We can also set by combining declaration and initialization in the following ways:

     dataType[] arrayRefVar = new dataType[arraySize];
     or
     dataType[] arrayRefVar = {value0, value1, ..., valuek};

    Accessing an array element:

    Array elements can be accessed by using its index number, where each element is assigned by one index number.
    Example:

    age[2] = 14; // insert 14 to third element

    Example of a one-dimensional array in Java:

      public class ArrayTest
      {    
        public static void main(String args[])
        {  
          int a[]={10,22,4,7};//declaration, instantiation and initialization
            
         //displaying array  
         for(int i=0; i < a.length; i++)  
            {
            System.out.println(a[i]);
            }
        }
      }

    Output:

      10
      22
      4
      7

  • Java – Jump Statement

    In Java, Jump statements are used to interrupt loop or switch-case instantly to transfer the program control from one point to elsewhere in the program.
    Java supports three jump statements:

    • continue. 
    • break 
    • return.

    break: 

    This statement is used within the loop or switch cases to terminate that statement or the current loop is immediately stopped and resumes at the next statement followed by a terminated loop. we can also say that it helps to break out from the middle of the loop.

    Syntax of break statement:

     break; 

    continue

    It is used in loops to jump immediately to the next iteration of the loop. continue is used with while loop or do/while loop and with for loop. When continue is used inside for loop then the control immediately jumps to the increment or decrement part of the for loop. And when used inside while loop or do/while loop then the control immediately jumps to the condition part.

    Syntax of continue statement:

    continue; 

    label break statement

    Java does not support goto as it leads to unstructured programming. so instead of ‘goto‘ we can use a label break statement which works like a goto statement.

    Syntax of label:

     label:
        for()
        {
          //code
          break label;
        }

    Example of label break statement in Java:

     public class LabelTest 
     { 
      public static void main(String[] args) 
      { 
        // label for outer loop 
        outerlabel: 
        for (int i = 0; i < 10; i++) 
        { 
        for (int j = 0; j < 10; j++)
        { 
            if (j == 3) 
            {
                break outerlabel; 
            }
                
            System.out.println(" value of j = " + j); 
        } 
        } 
       } 
     }

    Output:

     value of j = 0
     value of j = 1
     value of j = 2

  • Java – continue statement with Example

    It is one of the jump statement in java. It is used in loops to jump immediately to the next iteration of the loop. continue is used with while loop or do/while loop and with for loop.

    When continue is used inside for loop then the control immediately jumps to the increment or decrement part of the for a loop. And when used inside while loop or do/while loop then the control immediately jumps to the condition part.

    Flowchart for Java continue statement:

    Continue statement in java

    Syntax of continue:

    continue; 

    Example: Java program to demonstrate the continue statement:

     public class ContinueTest 
     {
        public static void main(String args[])
        {
        for (int i=0; i<=10; i++)
        {
         if (i == 5)
         {
          continue; // skip the rest of the code
         }
    
        System.out.print(i+" ");
        }
        }
     }

    Output:

     0 1 2 3 4 6 7 8 9 10 

  • Java – break statement with Example

    It is one of the jump statement in java.

    This statement is used within the loop or switch cases to terminate that statement or the current loop is immediately stopped and resumes at the next statement followed by a terminated loop. we can also say that it helps to break out from the middle of the loop.

    Flowchart for java break statement:

    Syntax of break:

     break; 

    Example: Java Program to demonstrate break statement:

     public class BreakTest 
     {  
        public static void main(String[] args) 
        {  
        //using for loop  
        for(int i=1; i<=15; i++)
        {  
            if(i==10)
            {  
              break;  //breaking the loop 
            }  
            System.out.println(i);  
        }  
        }  
     }

    Output

     1
     2
     3
     4
     5
     6
     7
     8
     9

  • Java – Loops with Example

    A loop statement allows us to execute a block of code or statement or group of statements as many times according to the user’s need. It is done by evaluating a given condition for true and false. The statement stops looping when the condition is false.
    There are two types of loops :

    1. Entry control loop: In this loop, the conditional is checked then the program is executed. There are two types of entry control loop.

    • for loop. 
    • while loop. 

    2. Entry control loop: In this loop, the program is executed first and then the condition is checked. There is only one type of loop in the exit control loop.

    • do-while loop.

    1. while loop: 

    The while loop is one of the fundamental loop statement in Java. It is an entry-control loop, that is it checks the condition at the beginning of the block. And repeats a statement or block until the condition is true. And the increment of the variable checked in condition checking is done inside the block as shown in the below example.

    Syntax of while loop:

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example: Click here


    2. do…while loop: 

    Sometimes it is necessary that the block or statement must be executed at least once but if the condition is initially at a false state then the block will not be executed. So for the situation where a block of code must be executed at least once a do-while loop comes in play.

    do-while is an exit-control loop that is it checks the condition after the first execution of the block. And it keeps executing the block until the condition state is false.

    Syntax of do..while loop:

    do
     {
      statements..
     }while (condition);

    Example: Click here


    3. for loop: 

    The Java for loop allows the user to iterate a part of the program multiple times. If a user is certain about how many specific numbers of times the loop must be executed then for loop is recommended. It is also an entry-control loop but here flow control contains three steps:

    • initialization = The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition = Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement = The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    Syntax of for loop:

     for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example: Click here


    4. Enhanced For loop:

    Java 5 added another version of for loop. It is useful to iterate through the elements of a collection or array.

    Syntax of enhanced for loop:

    for(declaration : expression) 
     {
      // Statements
     }

    Declaration – This is declared variable and must be compatible with the elements of collection or array that is executing. And is available only for that block.
    Expression – This is the name of an array you need to loop through.

    Example of enhanced for loop in Java:

    public class ForEachTest 
     {
      public static void main(String args[]) 
      {
        int  num[] = {5, 10, 15, 20, 25, 30};
    
        for(int a : num ) 
        {
            System.out.println( a );
            System.out.println("\n");
        }
      }
     } 

    Output:

     5
     10
     15
     20
     25
     30

    Another example enhanced loop in Java with String:

     public class ForEachTest 
     {
      public static void main(String args[]) 
      {
        String  name[] = {"John", "Sam", "Arya", "Brandon"};
    
        for( String singer : name ) 
        {
            System.out.print( singer );
            System.out.print("\n");
        }
            
      }
     } 

    Output:

     John
     Sam 
     Arya
     Brandon

  • Java – for loop with Syntax, Flowchart and Example.

    The Java for loop allows the user to iterate a part of the program multiple times. If a user is certain about how many specific numbers of times the loop must be executed then for loop is recommended. It is also an entry-control loop but here flow control contains three steps:

    • initialization = The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition = Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement = The third one is the increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    Flowchart of for loop in Java:

    for loop java

    Syntax of for loop:

    for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example of for loop in Java:

     public class ForTest 
     {
      public static void main(String args[]) 
      {
        for(int a = 1; a < 10; a++) 
        {
        System.out.println("value of a : " + a );
        }
      }
     }

    Output:

     value of a : 1
     value of a : 2
     value of a : 3
     value of a : 4
     value of a : 5
     value of a : 6
     value of a : 7
     value of a : 8
     value of a : 9

  • Java – While Loop with Example

    The while loop is one of the fundamental loop statements in Java. It is an entry-control loop that is it checks the condition at the beginning of the block. And repeats a statement or block until the condition is true. And the increment of the variable checked in condition checking is done inside the block as shown in the below example.

    Flowchart for While loop in Java:

    While Loop in Java

    Syntax of while loop:

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example of while loop in Java:

    public class WhileTest
    {
      public static void main(String args[]) 
      {
        int a = 1;
    
        while( a < 10 ) 
        {
            System.out.println("value of a : " + a );
            a++; //increment
            
        }
      }
    }

    Output of while Loop:

     value of a : 1
     value of a : 2
     value of a : 3
     value of a : 4
     value of a : 5
     value of a : 6
     value of a : 7
     value of a : 8
     value of a : 9

  • Java – do while Loop with Example

    Sometimes it is necessary that the block or statement must be executed at least once but if the condition is initially at a false state then the block will not be executed.
    So for the situation where a block of code must be executed at least once, a do-while loop comes in play.

    dowhile is an exit-control loop that is it checks the condition after the first execution of the block. And it keeps executing the block until the condition state is false.

    Flowchart for do-While loop in Java:

    Syntax of do..while loop:

    do
     {
      statements..
     }while (condition);

    Example of a do-while loop in java:

     public class dowhileloopTest
     { 
       public static void main(String args[]) 
       { 
        int a = 1; 
        do
        { 
           //will be executed once
           System.out.println("Value of x:" + a); 
           a++; 
        } 
        while (a < 5); 
      } 
     }

    Output:

     Value of x:1
     Value of x:2
     Value of x:3
     Value of x:4

  • Java – Switch Case Statement with Example

    A switch statement allows a variable to be tested for equality against multiple values. It provides the case for the different blocks of code to be executed.

    Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values. In the end, it can have a default case which is optional that is executed if no cases are matched.

    It can also have an optional break that is useful to exit the switch otherwise it continues to the next case.

    Flowchart for switch statement in java:

    switch statement

    Syntax of switch statements:

     {
        case value1:    
        //code to be executed;    
        break;  //optional  
        case value2:    
        //code to be executed;    
        break;  //optional  
        .
        .
        .
        .    
        case valueN:    
        //code to be executed;    
        break;  //optional 
            
        default:     
        code to be executed if all cases are not matched;    
      }

    Example of a switch-case statement in Java:

      public class SwitchTest 
      {  
        public static void main(String[] args) 
        {  
          int num = 5;  
          //Switch expression  
          switch(num)
          {  
            //Case statements  
            case 3: System.out.println("Number 3");  
            break;  
            case 5: System.out.println("Number 5");  
            break;  
            case 8: System.out.println("Number 8");  
            break;  
            //Default case
            default: System.out.println("Not matched");  
          }  
        }  
      }

    Output switch-case statement:

     Number 5

    For practice you can look at this article: Currency Conversion Program in Java