- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Statements
In the world of Java, statements are the basic building blocks of your code. Think of them as the complete sentences of a programming language. While an expression might represent a value (like 5 + 5), a statement performs a specific action (like int result = 5 + 5;).
Every Java program is essentially a sequence of statements executed by the Java Virtual Machine (JVM). Understanding how to structure these instructions correctly is the first step toward writing clean, functional software.
Expression Statements:
- These statements perform a specific task or calculation and must always end with a semicolon (;).
- Common types include variable assignments, incrementing/decrementing values, and calling methods.
- Real-world Example: Updating a user's account balance after a purchase or logging a message to the console.
x + y is an expression, but it isn't a statement because it doesn't "do" anything. To make it a statement, you must assign it or use it, like sum = x + y;.
Declaration Statements:
- These are used to define variables by specifying their data type and name.
- They "reserve" space in memory for your data. You can declare a variable and initialize it (give it a value) at the same time.
- Examples:
int age = 25;orString username;.
int d;, use int daysUntilExpiration;. This makes your statements much easier for other developers to read.
Control Flow Statements:
- By default, Java executes code line-by-line. Control flow statements allow you to break this linear path, making your program "smart."
- Conditional Statements: Use
if,else, andswitchto make decisions. For example, checking if a user is logged in before showing a dashboard. - Looping Statements: Use
for,while, anddo-whileto repeat actions, such as processing every item in a shopping cart. - Branching Statements: Use
break,continue, andreturnto jump to different parts of the code or exit a method.
do-while loop or accidentally putting a semicolon right after an if condition, like if (x > 10); { ... }, which effectively cancels the logic.
Block Statements:
- A block is a group of zero or more statements wrapped in curly braces {}.
- Blocks allow you to group multiple instructions so they can be treated as a single unit by control flow constructs.
- Variables declared inside a block have "local scope," meaning they generally cannot be accessed once the block ends.
if statement), you won't be able to use it outside that block.
Example
public class StatementsExample {
public static void main(String[] args) {
// --- Expression statements ---
int x = 10; // Assignment
int y = 20;
x++; // Increment statement
// Calling a method is also an expression statement
System.out.println("Current value of x: " + x);
// --- Declaration statement ---
int result;
// --- Control flow statement ---
// This directs the "flow" of the program based on a condition
if (x < y) {
result = x + y;
System.out.println("The sum is: " + result);
} else {
System.out.println("x is not less than y");
}
// --- Block statement ---
// Used here to create a specific scope for temporary variables
{
int tempMultiplier = 5;
int scopedProduct = x * tempMultiplier;
System.out.println("Scoped Product: " + scopedProduct);
}
// Note: tempMultiplier is not accessible here!
}
}
Summary
Java statements are the fundamental units of execution in your applications. By mastering expression statements for actions, declaration statements for data, control flow for logic, and blocks for organization, you gain full control over how your software behaves. Remember that consistent indentation and proper semicolon usage are key to writing statements that the Java compiler can understand and your teammates can maintain.