Java / Java 21 Coding Standards Interview Questions
How do you apply consistent indentation and brace style in Java 21 code?
Most Java style guides use four-space indentation (no tabs) and Kernighan-and-Ritchie style braces, where the opening brace stays on the same line as the declaration and the closing brace lines up with the start of that declaration.
public void processOrder(Order order) { if (order.isValid()) { submit(order); } else { reject(order); } }
Braces are required around every block, even a single-statement if, because an unbraced single statement is a common source of bugs when a second line is later added and silently falls outside the condition's scope.
Line length is typically capped at 100 or 120 characters, and continuation lines for a long method chain or parameter list are indented an extra level beyond the first line so the wrap is visually distinguishable from a new block.
More Related questions...