Skip to main content

Java Statements

Java is a high-level, class-based, object-oriented programming language. One of the most fundamental components of Java programming is the 'Java Statement'. Let's dive deeper into the world of Java statements and understand their importance in Java programming.

What are Java Statements?

In the simplest terms, Java statements are instructions that tell the computer what to do. They are the building blocks of any Java program. Each statement is an expression that can produce a value.

Java statements are categorized into three main types:

  1. Expression Statements: These are simple statements that include assignment expressions, increment or decrement expressions, method invocations, or object creation expressions.

  2. Declaration Statements: These are statements where a variable is declared, or an object is created.

  3. Control Flow Statements: These statements regulate the order in which the statements are executed. They include decision making (if-then, if-then-else, switch), looping (for, while, do-while), and branching statements (break, continue, return).

Expression Statements

Expressions are the most common type of statement. Here are some examples:

int a = 10; // assignment expression
a++; // increment expression
System.out.println(a); // method invocation expression

Declaration Statements

A declaration statement declares a variable and, optionally, also initializes the variable. Here is an example of a declaration statement:

int number = 10; // declaring an integer variable 'number' and initializing it with value 10

Control Flow Statements

Control flow statements determine the order in which the other statements get executed.

If-Then Statement: It is the most basic control flow statement. It tells your program to execute a certain section of code only if a particular test evaluates to true.

int number = 10; 
if (number > 0) {
System.out.println("Number is positive.");
}

For Statement: It provides a compact way to iterate over a range of values.

for(int i=0; i<5; i++) {
System.out.println(i);
}

Switch Statement: A switch statement allows a variable to be tested for equality against a list of values.

int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
/* ... */
default: System.out.println("Invalid day"); break;
}

In the next sections, we will delve deeper into each type of control flow statement, their syntax, and how they can be used in a Java program.

Java statements are essential for any Java program as they help in performing different types of operations, such as assigning values to variables, performing arithmetic operations, calling methods, looping, decision making, and much more.

Remember, each statement must end with a semicolon (;). It indicates the end of one logic entity.

Now that you have a basic understanding of Java Statements, try to experiment with different types of statements, and see how they affect the execution flow of your Java programs. Happy Coding!