Java Formatter

Format and indent Java code with proper brace handling and comment preservation.

Back to all tools on ToolForge

More in Developer Tools



Result

About Java Formatter

This Java formatter beautifies and indents Java code based on braces. It adds line breaks and indentation to make code easier to read without using external libraries.

It is useful for quickly formatting minified or poorly indented Java snippets, pasted code, or small examples before sharing or reviewing. The formatter handles strings, comments, and proper brace matching.

Java Code Style Guidelines

Oracle Java Code Conventions:

1. Indentation: 4 spaces per level (not tabs)
2. Line length: Maximum 80-120 characters
3. Braces: K&R style (opening brace same line)
4. Statements: One per line, end with semicolon
5. Blank lines: Separate methods and logical sections

Example formatted code:
public class HelloWorld {
    private static final int MAX = 100;

    public static void main(String[] args) {
        for (int i = 0; i < MAX; i++) {
            if (i % 2 == 0) {
                System.out.println("Even: " + i);
            } else {
                System.out.println("Odd: " + i);
            }
        }
    }
}

Comment Types in Java

// Single-line comment
int x = 10;

/* Multi-line comment
   spans multiple lines */
int y = 20;

/**
 * Javadoc comment for documentation
 * @param name the user name
 * @return greeting message
 */
public String greet(String name) {
    return "Hello, " + name;
}

Common Java Formatting Issues

IssueIncorrectCorrect
Missing indentpublic void m(){if(x){doSomething();}}public void m() {
if (x) {
doSomething();
}
}
No spacingfor(int i=0;i<10;i++)for (int i = 0; i < 10; i++)
Brace placementif (x)
doSomething();
if (x) {
doSomething();
}
Long lines200+ char linesBreak at 80-120 chars

Formatting Algorithm

How this formatter works:

1. Track state: inString, inComment, inLineComment
2. Process character by character
3. Handle special cases:
   - Strings: Preserve content, track escape chars
   - Comments: Preserve /* */ and // comments
   - Braces: Increase/decrease indent level
   - Semicolons: Add line break after
4. Add indentation at line starts
5. Handle edge cases: empty lines, consecutive braces

Limitations:
- Doesn't add spaces around operators
- Doesn't enforce line length limits
- Doesn't reorder imports
- Doesn't validate Java syntax

For production code, use IDE formatters or
google-java-format for complete formatting.

Best Practices for Readable Java

Frequently Asked Questions

What is Java code formatting?
Java code formatting applies consistent indentation and line breaks to make code readable. Proper formatting places opening braces on the same or new line, indents nested blocks (typically 2-4 spaces), and adds line breaks after statements. Follows conventions like Oracle Code Conventions or Google Java Style Guide.
What are Java naming conventions?
Java uses PascalCase for classes (MyClass), camelCase for methods/variables (myMethod, myVariable), and UPPER_SNAKE_CASE for constants (MAX_VALUE). Package names are lowercase (com.example.package). Following conventions improves code readability and maintainability.
What is the standard Java indentation?
Standard Java indentation is 4 spaces per level (not tabs). Each nested block (class, method, if, loop) adds one indent level. Closing braces align with the opening statement. Consistent indentation is crucial for readability, especially in deeply nested code.
How are Java comments handled?
Java has three comment types: single-line (// comment), multi-line (/* comment */), and Javadoc (/** ... */). Formatters preserve comments in their original positions. Javadoc comments appear before class/method declarations and support tags like @param, @return, @throws.
What is K&R vs Allman brace style?
K&R (kernel) style places opening brace on same line: if (x) { ... }. Allman style places it on new line: if (x) \n { ... }. Java typically uses K&R (Oracle style). Both are valid; consistency matters more than choice. This formatter uses K&R style.
What tools format Java code professionally?
Professional tools: IntelliJ IDEA (built-in formatter), Eclipse (Ctrl+Shift+F), google-java-format, Checkstyle, Spotless. IDEs apply style guides automatically. This simple formatter handles basic brace-based formatting for quick cleanup of minified or poorly indented code.