JAVA BASICS
Java statements
A program consists of instructions for a computer. In programming, these instructions are called statements.
What is a Java statement?
A statement tells a program to perform a particular action: create a variable, change its value, call a method or output information. The following statement outputs text to the console:
System.out.println("Java yra smagi!"); This calls the method println, and the text passed in parentheses appears on the screen. The semicolon marks the end of the statement.
A semicolon at the end of a statement
Many simple Java statements end with a semicolon ;. It helps the compiler identify where one instruction ends and the next begins.
If we remove the semicolon from the output statement, the code will not compile:
System.out.println("Java yra smagi!")error: ';' expected
A statement is like a sentence: an ordinary sentence ends with a punctuation mark, while a simple Java statement ends with a semicolon.
Not every construct ends with a semicolon. For example, an if, for, while, method or class block enclosed in braces usually has no extra semicolon after the closing brace.
Multiple statements
Real programs contain many statements. Within the same block, they usually execute from top to bottom, in the order they are written:
System.out.println("Labas, pasauli!");
System.out.println("Geros dienos!");
System.out.println("Mokytis Java yra smagu!");- The first statement outputs the text Labas, pasauli!
- The second statement outputs the text Geros dienos!
- The third statement outputs the text Mokytis Java yra smagu!
Each println call ends its output with a new line, so the console displays three separate lines.
What statements do
While learning Java, you will encounter statements with different purposes:
- Declaration statements create variables and specify their types.
- Assignment statements set or change a variable’s value.
- Method calls start a specific program operation.
- Control-flow statements choose or repeat the code to execute.
At first, focus on recognising the boundaries of each statement and remembering the semicolon where it is required.