Semicolons and Indentation Tutorial

All Java statements - except blocks - are terminated by using a semicolon. The Java compiler pays no attention to lines, so you can have numerous statements on one line or one statement on numerous lines. The semicolon lets the javac compiler know that a statement is complete. The code in between an opening brace { expressions, statements, etc. } and a closing brace is known as the code block. Code blocks should always have matching indentation to make your program more readable.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Semicolon
C:\Java>cd Semicolon
C:\Java\Semicolon>Notepad Semicolon.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Semicolon {
    public static void main(String args[]) {
        int x = 67; 
        int y = 92;
        int z = 105;
        System.out.println("x=" + x + ", y=" + y + ", z=" + z);
        
        int a = 4;int b=58;int c=87;System.out.println("a="+a+", b="+b+", c="+c);

        System.out.println("As you "+
            "can see" + " the compiler" +
            " doesn't care about lines."); System.out.println();
System.out.println("Lines and indentation make a program readable "+
"and easier to understand.");

    }
}

Now switch back to the command prompt (CMD) and type in javac Semicolon.java and press Enter.
Now type in java Semicolon and press Enter.


C:\Java\Semicolon>javac Semicolon.java
C:\Java\Semicolon>java Semicolon
x=67, y=92, z=105
a=4, b=58, c=87
As you can see the compiler doesn't care about lines.

Lines and indentation make a program readable and easier to understand.


Final thoughts

Semicolons simply terminate statements. Lines mean nothing to the javac compiler. Practicing proper indentation will make your code more readable, especially when your code blocks contain a lot of statements.


Tutorials