Javatpoint Logo
Javatpoint Logo

Java Indentation

In programming, indentation is just like formatting. It is used to make the code readable to other users because it makes the code easier to edit, displays how the braces match up, and shows the logic of the program in well-organized fashion. It signals to the reader that he/ she is about to dive into another a new section of a program.

Generally, the unit of indentation is 4 spaces. Tabs expand to 8 spaces.

  • Always keep line lengths < 80 chars.
  • Breaking lines:
    1. After a comma or before an operator
    2. Try making lines close to 80 chars before breaking
    3. The new line must be aligned at the beginning of the previous line expression.
    4. If the above lead to messy code, just indent to 8 spaces.

The goal of the indentation is to show the logic of the program. We must keep indentation throughout the program because it is an essential part of formatting.

For statements inside a left and right brace, increase the indenting by two spaces. When another pair of braces is nested inside those braces, increase the indentation by another two spaces. It is a good impression to line up the braces vertically.

Remember that the function of indentation is to make the program more readable and understandable that saves an immense amount of time when editing or refining the code.

The braces used in Java follows the Kernighan and Ritchie style (K & R Style) to indent non-empty blocks and blocks like construct.

  • No line break before the opening brace.
  • Line break after the opening brace.
  • Line break before the closing brace.
  • Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, constructor, or named class. For example, there is no line break after the brace if it is followed by else or a comma.

Let's consider the following code snippet.

An empty block or block-like construct may be in K & R style. On the other hand, it may be closed immediately after it is opened, with no characters or line break in between ({}), unless it is part of a multi-block statement (like try/ catch, and finally). For examples, consider the following code snippet.

Keep opening brace at the same line. Every next contained block needs to be indented, so it is easier to read when viewing the code. Remember that if we are building commercial software, each line of code is written once, but read 2 - 6 times, so we will do good to other people by properly formatting the code.

Strring.indent() Method

Java also provides indent() method that provides the indentation for the strings based on the count value. It also normalizes line termination characters.


Where count denotes the number of white spaces to add or remove. Note that the value of count can be positive or negative. Positive value denotes adding space and negative value denotes removing space. It automatically adds a newline character if it hasn't been provided yet.

  • If the value of count is greater than 0, the spaces are added to the beginning of each line.
  • If the value of count is less than 0, the spaces are removed from the beginning of each line.
  • If the value of count is greater than white spaces then all the leading spaces are removed.
  • If the value of count is equal to 0, it will only change the line terminator.


Java indentation

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA