4.2 Expression, Declaration, Code Block and Method

In the previous video we covered keywords and expressions. This lesson focuses on three more building blocks of Java: declarations, whitespace and indentation. A declaration combines a type, a name and usually a value. The line int var = 2; is a full declaration. It starts with the data type, then the variable name and an expression on the right of the equal sign, and is terminated by a semicolon.

The semicolon is the boundary marker for a Java statement. You can also chain operations like var++ or var--, which you have already seen with arithmetic operators. Several declarations can technically be written on the same line, but it is usually confusing, so the recommendation is one declaration per line. The compiler does not care: as long as the semicolons mark the end of each statement, the program works.

Whitespace and indentation

  • Java ignores extra whitespace between tokens, so you can space out your code freely.
  • You need at least one space between a type and its variable name, for example between int and var.
  • Indentation is not enforced by the compiler but makes the structure of if/else blocks and curly braces obvious to humans.

If you write if (var == 10) System.out.println("yes"); else System.out.println("no"); on a single line, the program still runs but it is much harder to read. IntelliJ can format the code automatically, breaking lines and indenting the body of each block. The next lesson moves on to code blocks and the keywords that drive control flow.

Summary

This lesson covers the fundamental building blocks of Java code: expressions, declarations, whitespace rules, and code organization. You'll learn proper variable declaration syntax (data type + variable name + value + semicolon), understand that Java ignores whitespace internally, and discover why proper indentation and code block formatting are essential for readability and program logic flow.

Key points

  • A valid Java declaration requires three components: data type, variable name, initial value (optional), and ends with a semicolon
  • Java ignores whitespace (spaces, indentation) internally, allowing flexible formatting while maintaining syntax validity
  • Proper indentation and line breaks are critical for human code readability and understanding program flow, even though Java doesn't require them
  • Multiple variable declarations should be placed on separate lines rather than combined on one line to avoid confusion
  • Code blocks and method organization use indentation to logically group statements and improve code maintainability

FAQ

What is the difference between an expression and a declaration in Java?

An expression (such as var=5) is a computation or value assignment, while a declaration is a complete statement that ends with a semicolon. A declaration includes the data type, variable name, and initial value (e.g., int var = 5;).

Does Java care about whitespace and indentation?

Java ignores whitespace internally, so extra spaces are treated as single spaces. However, proper indentation and spacing are crucial for human readability and code maintainability, making programs easier to understand and modify.

Can I write multiple variable declarations on the same line?

While technically possible, it's not recommended because it becomes confusing and difficult to read. Best practice is to place each declaration on a separate line for clarity.