Java Blocks, Expressions and Statement with Examples

Here, we have discussed about Java Blocks, Java Expressions and Java statement. In Java, blocks are the statement or set of code which are written...

Welcome to the another interesting article on Java Programming Languages

Java Blocks, Java Expressions and Java statement

In this article you will learn about Java Blocks, Java Expressions and Java statement with the help of examples.

Java Blocks

In Java, blocks are the statement or set of code which are written within the Curly braces {}. We can write code between the opening brace { and closing brace } and it can be one group or more than one statements.

Types of Blocks in Java

There are two types of Java blocks that are explained as follows;

Types of Blocks in Java

Static Blocks

In Java, Static Blocks are those blocks in which the code block is declared with the static block. And it can be executed only one time for the program life cycle.

Non-Static Blocks

In Java, Non-Static are those blocks in which the code block is not declared with static block. And it can be executed multiple times according to the requirements of the user.

Example of Java Blocks;

package test;

public class JavaBlocks{

  // Java static block

  static {

    System.out.println("This is Java static block");

  }

  // constructor of Java Blocks

  JavaBlocks(){

    System.out.println("This is the constructor of JavaBlocks");

  }

  // Java non-static block 

  {

    System.out.println("This is java non static block");

  }

  public static void main(String[] args) {

    System.out.println("This is the main method 1");

    JavaBlocks obj1=new JavaBlocks();

    JavaBlocks obj2=new JavaBlocks();

  }

}

Java Expressions

In Java, expressions are made up by using variables, method calls, literals and operators. Mostly Java expressions are used to create a new value but it can also assign value to the variables.

For example;

int numeric; 

numeric = 20;

Here, is an expression. Let's understand by considering another example;

if (number1 == number2)

    System.out.println("Number 2 is smaller than number 3");

In above example, is an expression which return with a boolean value. Likewise,  is a string expression.

Java Statement

In Java, statement is a complete execution unit which commands the what to do to the compiler. A Java statement can include one or more than one expressions.

Types of Java Statement

In Java, there are three types of statements that are explained as follows;

Types of Java Statement

Expression Statement 

Expression statements are those statements which are used to change variable's value, call methods, and in creating new objects.

Declaration Statement

Declaration statements are those statements which are used to declare variables.

Control-flow Statement

Control-flow Statements are those statements which are used to determine the order in which the statements will be executed.

Example of  Java Statement;

  //declaration statement

int number;

 //expression statement

number = 7;

 //control flow statement

if (number < 10 )

{

  //expression statement

  System.out.println(number + " is less than ten");

}

Conclusion

Above we have discussed about Java Blocks, Java Expressions and Java statement. In Java, blocks are the statement or set of code which are written within the Curly braces {}. Java expressions are made up by using variables, method calls, literals and operators. Java statement is a complete execution unit which commands the what to do to the compiler. I hope this information is helpful to you all.