Java Formatter

Re-indent Java code by brace depth.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What the Java Formatter does

The Java Formatter re-indents Java code: each line is placed four spaces in for every unclosed {, ( or [ before it. It tidies code whose indentation was lost when it was pasted or copied from a chat or a PDF.

It reads Java strings, character literals, comments and text blocks (""" … """), so a brace inside them does not change the indentation, and lines inside a multi-line string are left exactly as written. It does not rewrap lines or change spacing within a line.

Java Formatter examples

The built-in sample

Input

public class Greeter {
private final String name;
public String greet(int n) {
if (n > 1) {
return "Hello all";
} else {
return "Hello " + name;
}
}
}

Output

public class Greeter {
    private final String name;
    public String greet(int n) {
        if (n > 1) {
            return "Hello all";
        } else {
            return "Hello " + name;
        }
    }
}

This is the input that the Load sample button fills in.

Braces inside strings and comments

Input

class Box {
String open = "{";
// a } in a comment
char close = '}';
String json = """
  {"a": 1}
  """;
void show() {
run(open, close);
}
}

Output

class Box {
    String open = "{";
    // a } in a comment
    char close = '}';
    String json = """
  {"a": 1}
  """;
    void show() {
        run(open, close);
    }
}

The braces inside strings and comments are ignored, so the following lines are indented correctly.

Switch statements

Input

switch (x) {
case 1:
foo();
break;
}

Output

switch (x) {
    case 1:
    foo();
    break;
}

Case labels are indented like any other line inside the braces: only brackets change the depth.

Each output was produced by running this tool on the input above it.

How it works

Each line is trimmed and indented by the number of brackets ({, ( and [) still open before it. A line that starts with a closing bracket moves out one level first, so } else { lines up with its if.

While reading, the tool skips over strings and comments using the Java rules. Lines that begin inside a multi-line string are copied unchanged, because changing them would change the string. Lines inside a block comment are re-indented, and a leading * is lined up under the opening /*. Runs of blank lines become one.

Details and limits

  • Your line breaks are kept: code that is all on one line comes back on one line.
  • The tool does not check syntax. An unclosed string or bracket shifts the lines after it, which is often a quick way to find one.
  • For a full reformat, with line wrapping and spacing rules, use google-java-format or your IDE.

Common errors

SymptomWhat it means
Everything after a line is indented too farA bracket or string on that line is not closed. Check the line where the indentation starts to drift.

Questions

Is this a full Java formatter?

No. It fixes indentation by counting braces and keeps your line breaks. For line wrapping, import ordering and spacing rules, use the formatter in your IDE or a tool such as google-java-format.