Whitespace in Java
In the Java programming language, whitespace does not affect the functionality of code. Instead, Java uses semicolons to mark the end of every statement.
As an extreme example, this means that we could write a Java application on a single line, with each statement distinguished only by semicolons!
System.out.println("Java is a fun language to learn.");System.out.println("Every statement ends in a semicolon.");System.out.println("We don't need to include any whitespace, but it improves readability!");
Output:
Java is a fun language to learn.
Every statement ends in a semicolon.
We don’t need to include any whitespace, but it improves readability!
This program is very difficult to read. The more functionality it has and the more complex it becomes, the more difficult it would be to read and work with.
For this reason, even though we aren’t programmatically required to use whitespace throughout our Java programs, it makes it much easier to read and debug. It also helps us adhere to standards for code readability, allows Java code to resemble that of other languages that programmatically rely on whitespace. This makes everyone’s life easier!
Let’s see how the above example looks just by placing each statement on an individual line:
System.out.println("Java is a fun language to learn.");
System.out.println("Every statement ends in a semicolon.");
System.out.println("We don't need to include any whitespace, but it improves readability!");
This is much easier to read. We can instantly tell that the code consists of a series of print statements, without having to carefully parse through a thick block of text!