Java Statements
A computer program works by following a series of commands provided by the
programmer. In Java, each individual command is known as a statement.
A statement represents a single action that the Java compiler understands
and executes.
Example of a Java Statement
The statement below instructs Java to display a message on the
screen:
System.out.println("Coding in Java is exciting");
Important Rule:
Every Java statement must end with a semicolon (;).
The semicolon indicates that the instruction is complete.
Error Caused by Missing Semicolon
If a semicolon is omitted, the compiler cannot understand where the
statement ends.
Incorrect Code Example
System.out.println("Coding in Java is exciting")
Compiler Output
error: ';' expected
Think of a Java statement as an English sentence:
- English sentences end with a full stop (.)
- Java statements end with a semicolon (;)
Multiple Statements in Java
Most real-world Java programs include several statements. These statements
are executed sequentially, meaning Java runs them from top to bottom.
Example Program with Multiple Statements
public class StatementDemo {
public static void main(String[] args) {
System.out.println("Application
started");
System.out.println("Processing
instructions");
System.out.println("Application
finished");
}
}
Output
Application started
Processing instructions
Application finished
Java Displaying Texts
Java provides simple methods to show text or values on the screen. One
commonly used method is println(), which outputs text and then moves the
cursor to a new line.
Example Using println()
System.out.println("Welcome to Java programming");
This statement prints the message and automatically starts a new line after
displaying it.
Printing Multiple Lines
You can use multiple println() statements to display several lines of text.
Each call prints its content on a separate line.
Example
System.out.println("Java basics");
System.out.println("Output methods");
System.out.println("Learning step by step");
Output
Java basics
Output methods
Learning step by step
Using Double Quotation Marks
In Java, all text (also called a String literal) must be enclosed within
double quotes ("").
Correct Usage
System.out.println("This line will execute correctly");
Incorrect Usage
System.out.println(This line will cause an error);
The second statement will fail because the text is not wrapped in double
quotes.
The print() Method
Java also offers the print() method, which works similarly to println().
The key difference is that print() does not move to a new line after
displaying the output.
Example Using print()
System.out.print("Java output ");
System.out.print("appears on one line");
Output
Java output appears on one line
Java Displaying Numbers
Java is not limited to printing text—it can also display numeric values
directly to the output screen. The println() method works with numbers just
as easily as it does with text.
Printing Numeric Values
When printing numbers, do not use double quotation marks. Numbers are
treated as values, not text.
Example
System.out.println(10);
System.out.println(425);
System.out.println(98765);
Output
10
425
98765
Performing Calculations While Printing
Java allows you to include arithmetic expressions inside the println()
method. The calculation is performed first, and the result is then
printed.
Addition Example
System.out.println(12 + 8);
Output
20
Multiplication Example
System.out.println(7 * 6);
Output
42