Java Sequence of Instructions

In this unit, we will learn a few more concepts of Java and understand the order of arithmetic calculations.

1. Reassigning the Variables

The values of variables in Java can also be reassigned (modified) as in Python

Example:

class Main {

    public static void main(String[] args) {

        int a = 1;

        System.out.println(a);

        a = 2;

        System.out.println(a);

    }

}

 

Output:

1

2

 

In the above program, the value of the variable a is reassigned to 2

2. The final Keyword

The final keyword is used for variables, classes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...)

 

2.1 The final Variables

When the final keyword is used with a variable, it indicates that the variable is constant and the value of it cannot be reassigned (cannot be changed)

Example 1:

class Main {

    public static void main(String[] args) {

        final int SPEED_LIMIT = 60;

        System.out.println(SPEED_LIMIT);

    }

}

 

Output:

60

 

Naming Convention: The naming convention for the final variables is a bit different from normal variables. Generally, programmers follow uppercase letters with words separated by underscore(_). For example, SPEED_LIMIT, MAX_PRICE, etc.

What if the final variable value is changed?

class Main {

    public static void main(String[] args) {

        final int SPEED_LIMIT = 60;

        SPEED_LIMIT = 90;

        System.out.println(SPEED_LIMIT);

    }

}

 

Output:

file.java:4: error: cannot assign a value to final variable SPEED_LIMIT

      SPEED_LIMIT = 90;

      ^

1 error

 

An error will be thrown. As we learned earlier the value of final variables cannot be changed

 

3. Expression

An expression is a valid combination of values, variables and operators

Examples:

  • a * b

  • a + 2

  • 5 * 2 + 3 / 4

 

 

3.1 BODMAS

The standard order of evaluating an expression

  • Brackets (B)

  • Orders (O)

  • Division (D)

  • Multiplication (M)

  • Addition (A)

  • Subtraction (S)

 

Example:

class Main {

    public static void main(String[] args) {

        System.out.println(10 / 2 + 3);

        System.out.println(10 / (2 + 3));

    }

}

 

Output:

8

2

 

4. Statements

A statement forms a complete unit of execution. In Java, the expressions can be made into a statement by terminating the expression with a semicolon (;)

class Main {

    public static void main(String[] args) {

        int a = 1;  // definition statement

        System.out.println(a);  // method invocation statement

    }

}

Output:

1

Statement without semicolon (;)

 

Example

class Main {

   public static void main(String[] args) {

      int a = 1

      System.out.println(a);

   }

}

 

Output

file.java:3: error: ';' expected

      int a = 1

            ^

1 error

 

5. Block of Code

A Block of code consists of zero or more statements enclosed between balanced brackets ({})

Example:

class Main { // begin block 1

    public static void main(String[] args) {  // begin block 2

        int a = 1;

        System.out.println(a);

    }  // end block 2

}  // end block 1

 

5.1 Indentation

Indentation refers to the spaces at the beginning of a code line. It increases the code readability. In Java, as a best practice, we use four spaces for indentation.

5.2 Missing curly braces ({ or })

Example:

class Main {

    public static void main(String[] args) {

        int a = 1;

        System.out.println(a);

    }

Output:

file.java:5: error: reached end of file while parsing

   }

   ^

1 error

 

In the above program, the closing curly brace } is missing for the Main class (i.e, the braces are not balanced).

Conclusion

  • In Java, variables can be reassigned.

  • The final variables cannot be reassigned.

  • BODMAS: The standard order of evaluating an expression.

  • In Java, statements are terminated with a semicolon(;).

In Java, a block of code is enclosed between balanced brackets ({})