Whitespace and newlines in Java & Example

Whitespace and newlines in Java & Example

I have provided a summary of the handling of Whitespaces and line breaks in Java programs on this post.

Although you may not be aware of this in your daily life, it is an essential concept that you should grasp.

Whitespace and newlines in Java

Let's quickly review how Java outputs Whitespace and newlines.

Which programs cannot be executed?

Let's start with a quiz, albeit an abrupt one.

The following is a sample program. 

It is a simple program that outputs the characters "Hello World".

I tried to write this program differently and created the following three types of programs. Which of these programs cannot be executed?

(1) Program that has a new line at each phrase.

(2) Program with no Whitespaces at the beginning of sentences and line breaks in places.

(3) Program with random blanks and line breaks.

The correct answer is (3). Only (3) cannot be executed among these three types of programs.

Whitespace and Newline Characters in Java

Let me explain why programs (1) and (2) work and only program (3) does not.

Program (1)

Looking first at Program (1), you can see that it is a challenging program to read because of many line feeds. However, this program works correctly.

This is because Java allows you to write line breaks as you like, as long as they are in phrases.

The conversion of a Java source program into an executable form is called "compilation," The program that performs the conversion process is called a compiler. Even if it is difficult for humans to read, the compiler can understand it, so there is no problem.

Program (2)

How about Program (2)? As in (1), there are line breaks and no Whitespaces between sentences, giving the impression that it is difficult to read, but in this case, there is no problem because the compiler can understand the program.

Program (3)

In the case of (3), a Whitespace or line break is set in the middle of a clause. Since the clause cannot be understood, the compiler cannot understand what instructions are written. Therefore, a compile error occurs, and the program cannot be executed.

Devices to make it easier to read are necessary.

Although Whitespace and line breaks can be freely set in phrases, the human being writes the program. Therefore, it is essential to write programs that are easy for humans to read.

Let's look at the first sample program again.

Whitespaces are set before the method, and when writing the processing of the method, more Whitespaces are inserted to make it easier to read. 

Indentation is the process of setting a Whitespace at the beginning of a sentence, and coding with indentation makes it possible to write easy-to-read source code.

Also, if one sentence of the program is too long, it is necessary to scroll the editor to the right to read it since it becomes difficult to read while scrolling. 

It is recommended to write the program that can be read without scrolling by setting line breaks in some cases.

Summary

This post has provided a summary of Whitespace and line breaks in Java programs.

Although you may not usually think about them deeply, there may be situations where it is helpful to have a grasp of them.

Java Book for Beginner

The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts. 

It is also the best book to learn java and start your learning journey with Java Development. 

Head First Java covers almost all OOPS concepts and fascinatingly explains them. 

Despite several readers claiming it to be a dated book, as it covers nothing beyond Java 5.0, Head First Java is still found on the shelves of numerous Java veterans.

Thus, it is a must-have book for every Java pursuer and developer.