Video Lecture – 1?

1 - Introduction and Installing the java (JDK) Step by Step Tutorial 2 - Installing Eclipse IDE and Setting up Eclipse 3 - Creating First Java Project in Eclipse IDE

Video Lecture – 2?

4 - Variables and Types in Java 5 - Getting User Input using Java

Video Lecture – 3?

6 - Math and Arithmetic Operators in Java 7 - Increment Operator and Assignment Operator 8 - IF ... ELSE Statements and Relational Operators 9 - Logical Operators in Java

Video Lecture – 4?

10 - switch Statement in Java 11 - The while Statements (while Loops) 12 - The do-while Statements (do-while Loops)

Video Lecture – 5?

13 - Arrays in Java 14 - The for Statement in Java (for loops) 15 - Java String 16 - Introduction to Methods

Video Lecture – 6?

17 - Parameter passing and Returning a Value from a Method 18 - Classes and Objects in Java 19 - Class Constructor in Java 20 - Method Overloading in Java

Video Lecture – 7?

21 - 'static' keyword in Java 22 - 'static' keyword Example in Java 23 - Public, Private, Protected and this (Java Access Modifiers) 24 - The final keyword in Java

Video Lecture – 8?

25 - Inheritance in Java 26 - Polymorphism in Java 27 - Method Overriding in Java 28 - Abstract Methods and Classes

Video Lecture – 9?

29 - Java Interfaces 30 - Recursion in java 31 - Arraylist in Java 32 - LinkedList in Java 33 - Difference between LinkedList vs ArrayList in Java

Video Lecture – 10?

34 - ListIterator in Java 35 - HashSet in Java 36 - Catching and Handling Exceptions in Java using Try Catch Blocks 37 - Java Finally block (try-catch-finally Exception Handling in Java )

Video Lecture – 11?

38 - Create a File and Write in it Using PrintWriter and File class 39 - How to Read file using Java 40 - Using Date & Time + formatting Date using SimpleDateFormat

Revision

Java Flow Control

Java Arrays

Java OOP (I)

Java OOP (II)

Java OOP (III)

Java Exception Handling

Java List

Java Queue

Java Map

Java Set

Java I/O Streams

Java Reader/Writer

Additional Topics

Java Expressions, Statements and Blocks

In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples.

In previous chapters, we have used expressions, statements, and blocks without much explaining about them. Now that you know about variables, operators, and literals, it will be easier to understand these concepts.


Java Expressions

A Java expression consists of variables, operators, literals, and method calls. To know more about method calls, visit Java methods. For example,

int score; 
score = 90;

Here, score = 90 is an expression that returns an int. Consider another example,

Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;

Here, a + b - 3.4 is an expression.

if (number1 == number2)
    System.out.println("Number 1 is larger than number 2");

Here, number1 == number2 is an expression that returns a boolean value. Similarly, "Number 1 is larger than number 2" is a string expression.


Java Statements

In Java, each statement is a complete unit of execution. For example,

int score = 9*5;

Here, we have a statement. The complete execution of this statement involves multiplying integers 9 and 5 and then assigning the result to the variable score.

In the above statement, we have an expression 9 * 5. In Java, expressions are part of statements.


Expression statements

We can convert an expression into a statement by terminating the expression with a ;. These are known as expression statements. For example,

// expression
number = 10
// statement
number = 10;

In the above example, we have an expression number = 10. Here, by adding a semicolon (;), we have converted the expression into a statement (number = 10;).

Consider another example,

// expression
++number
// statement
++number;

Similarly, ++number is an expression whereas ++number; is a statement.


Declaration Statements

In Java, declaration statements are used for declaring variables. For example,

Double tax = 9.5;

The statement above declares a variable tax which is initialized to 9.5.

Note: There are control flow statements that are used in decision making and looping in Java. You will learn about control flow statements in later chapters.


Java Blocks

A block is a group of statements (zero or more) that is enclosed in curly braces { }. For example,

class Main {
    public static void main(String[] args) {
    	
        String band = "Beatles";
    	
        if (band == "Beatles") { // start of block
            System.out.print("Hey ");
            System.out.print("Jude!");
        } // end of block
    }
}

Output:

Hey Jude!

Let’s try the above code :

In the above example, we have a block if {....}.

Here, inside the block we have two statements:

  • System.out.print("Hey ");
  • System.out.print("Jude!");

However, a block may not have any statements. Consider the following examples,


class Main {
    public static void main(String[] args) {
    	
        if (10 > 5) { // start of block	
 
        } // end of block
    }
}

This is a valid Java program. Here, we have a block if {...}. However, there is no any statement inside this block.

class AssignmentOperator {
    public static void main(String[] args) {  // start of block 	

    } // end of block
}

Here, we have block public static void main() {...}. However, similar to the above example, this block does not have any statement.