Java Statements
Understanding different types of statements in Java programming
📋 Java Statements
Java statements are instructions that tell the program what to do. They include variable declarations, assignments, method calls, and control flow statements that execute sequentially.
// Different types of Java statements
public class StatementExample {
public static void main(String[] args) {
int age = 25; // Declaration statement
age = age + 1; // Assignment statement
System.out.println("Age: " + age); // Method call statement
}
}
Output:
Age: 26
Types of Statements
Declaration
Create variables and constants
int number;
String name = "John";
Assignment
Assign values to variables
number = 42;
name = "Jane";
Method Call
Execute methods and functions
System.out.println("Hello");
Math.max(10, 20);
Control Flow
Control program execution
if (condition) { }
for (int i = 0; i < 10; i++) { }
🔹 Declaration Statements
Declare variables to store data:
// Variable declarations
int age; // Declare integer variable
String name; // Declare String variable
double salary; // Declare double variable
boolean isActive; // Declare boolean variable
// Declaration with initialization
int count = 0; // Declare and initialize
String message = "Hello"; // Declare and assign value
double price = 99.99; // Declare with decimal value
boolean found = true; // Declare with boolean value
// Multiple declarations
int x, y, z; // Declare multiple variables
int a = 1, b = 2, c = 3; // Declare and initialize multiple
Example Usage:
Variables declared and ready to use
count = 0, message = "Hello", price = 99.99
🔹 Assignment Statements
Assign values to existing variables:
public class AssignmentExample {
public static void main(String[] args) {
// Simple assignments
int number = 10;
number = 20; // Reassign new value
number = number + 5; // Use current value in assignment
// String assignments
String greeting = "Hello";
greeting = greeting + " World"; // Concatenation assignment
// Compound assignments (shortcuts)
int total = 100;
total += 50; // Same as: total = total + 50
total -= 20; // Same as: total = total - 20
total *= 2; // Same as: total = total * 2
total /= 4; // Same as: total = total / 4
System.out.println("Number: " + number);
System.out.println("Greeting: " + greeting);
System.out.println("Total: " + total);
}
}
Output:
Number: 35
Greeting: Hello World
Total: 65
🔹 Expression Statements
Statements that evaluate expressions:
public class ExpressionExample {
public static void main(String[] args) {
int a = 10, b = 5;
// Arithmetic expressions
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus (remainder)
// Increment/Decrement expressions
a++; // Post-increment: a = a + 1
++b; // Pre-increment: b = b + 1
a--; // Post-decrement: a = a - 1
--b; // Pre-decrement: b = b - 1
// Method call expressions
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
System.out.println("Final a: " + a + ", b: " + b);
}
}
Output:
Sum: 15
Product: 50
Final a: 10, b: 5
🔹 Block Statements
Group multiple statements together:
public class BlockExample {
public static void main(String[] args) {
int score = 85;
// Block statement with if condition
if (score >= 80) {
// This is a block - multiple statements grouped
System.out.println("Excellent score!");
String grade = "A";
System.out.println("Grade: " + grade);
int bonus = 10;
System.out.println("Bonus points: " + bonus);
}
// Block statement with for loop
System.out.println("Counting:");
for (int i = 1; i <= 3; i++) {
// Another block - executed multiple times
System.out.println("Count: " + i);
int doubled = i * 2;
System.out.println("Doubled: " + doubled);
}
}
}
Output:
Excellent score!
Grade: A
Bonus points: 10
Counting:
Count: 1
Doubled: 2
Count: 2
Doubled: 4
Count: 3
Doubled: 6
🔹 Empty Statements
Sometimes you need a statement that does nothing:
public class EmptyStatementExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int target = 3;
int index = 0;
// Empty statement in a loop
// Find index of target number
while (index < numbers.length && numbers[index] != target) {
index++; // Only increment, no other action needed
}
if (index < numbers.length) {
System.out.println("Found " + target + " at index " + index);
} else {
System.out.println(target + " not found");
}
// Empty statement with semicolon only
; // This is an empty statement - does nothing
}
}
Output:
Found 3 at index 2