Lesson 5
Java Statements
An introduction to Java statements and command-line arguments.
Lesson content
Java Statements
Statements are similar to sentences in English. A Java statement is an instruction that explains what should happen.
Types of Java statements
- Expression statements: change values of variables, call methods, and create objects.
- Declaration statements: declare variables.
- Control-flow statements: determine the order in which statements are executed.
Control-flow statement groups
- Selection statements: if, if else, switch
- Iteration statements: for, while, do-while
- Jump statements: break, continue, return
Command-line arguments
A command-line argument is information passed after the Java program name during execution. It is stored as strings in the String array passed to main(). The args length tells us how many command-line arguments were provided.
If you do not check the args length, the program can crash when the user runs it with too few command-line arguments.
class command {
public static void main(String a[]) {
System.out.println("Welcome to java programming");
}
}First Java program
Our first Java program is written inside a class because Java is an object-oriented language. The main method is the execution point of the program, and System.out.println prints the output.
class command {
public static void main(String args[]) {
System.out.println("Welcome to java programming");
}
}Welcome to Java programming is the output produced by the example program.