Line At A Time
Lesson 1: Variables
Variables Store a whole number in a variable and print it.
1public static void main(String[] args) {
2 int five = 5;
3 System.out.println(five);
4}
Terminal
$ javac Main.java
$ java Main
5
Every line, explained
public static void main(String[] args) {
Every Java program starts at the main method. When you run the program, Java looks for this exact line, public static void main(String[] args), and runs everything between its { and } from top to bottom.
int five = 5;
int declares a variable that stores a whole number (an "integer") like 1, 5 or 1000. The = sign stores the value on the right into the variable named on the left, and the semicolon ; ends the statement, like a full stop at the end of a sentence. Here the variable is called five and it stores the value 5.
System.out.println(five);
System.out.println(...) prints whatever is inside the parentheses to the terminal, then moves to a new line. Read the name carefully: print-l-n is short for "print line". Here it prints the value stored in five, so the terminal shows 5.
}
This closing curly brace } ends the main method. In Java every opening brace { must have a matching closing brace }.