Java Code Formatter

Paste Java and lay it out with the four selectors. Only the whitespace between tokens is rewritten, so text blocks, literals, annotations and comments keep their content — and nothing is uploaded.

Runs locally in your browser
Copy

How to lay out Java code in the browser

Paste Java source and the page returns the same program with consistent indentation, one statement per line, a brace style you pick and optional line wrapping. Only the whitespace between tokens is rewritten, so text blocks, string and character literals, numeric literals such as 10_000L or 0b1010, annotations and comments keep their content.

Everything runs in this browser tab: no code is uploaded, the page keeps working offline, and nothing is written to disk. The formatter does not compile or validate — a file that does not compile comes back laid out, not repaired.

  1. Paste or type Java into the editor.
  2. Choose indentation, blank-line, wrapping and brace-style options; the result is rebuilt whenever a selector changes.
  3. Press Java Code Formatting, or press Ctrl / Cmd + Enter inside the editor.
  4. Press Copy to take the result, then Clear to empty both panes.

What the formatter rewrites — and what stays untouched

Whitespace only — the program cannot change

The source is read with Java's own lexical rules: line and block comments, javadoc, text blocks, character literals, string escapes, numeric literals with 0x / 0b prefixes, underscore separators and l / f / d suffixes, annotations, and every operator. A lambda arrow stays ->, a switch arrow stays ->, and generics come back tight — Map<String, List<Integer>>, not the padded Map < String > that an expression-oriented beautifier produces.

The output carries exactly the tokens of the input, in the same order. That is what keeps """ text blocks, @SuppressWarnings({"unchecked"}) and literals such as .5f, 10_000L or 0x1.8p1 intact. Formatted output of a 15-file corpus — text blocks, records, sealed interfaces, switch expressions, pattern matching — re-compiles with javac 21. One deliberate exception: comment continuation lines are re-indented with the code around them.

The four options, in practice

Indentation is a tab or 2, 3, 4 or 8 spaces. Blank lines can be removed, capped at one or two, or kept as written. Wrapping is off by default and can break longer lines at 80 or 120 characters; breaks fall after commas and before operators, and the continuation is indented one level.

Brace style follows the selector: K&R keeps the opening brace on the same line, Allman puts it on its own line, and the third option only moves else, catch and finally to a new line. In Chrome, a 58 KB Java file is laid out in about 10 ms and a 4 MB generated file in about 0.5 s.

What it does not do

There is no compiler, no syntax repair and no language-level check: text blocks require Java 15+, switch expressions 14+ and records 16+. A fragment that does not compile comes back laid out and still does not compile.

It also does not follow a full style guide: imports are not reordered, long boolean chains are not split beyond the wrap width, and chained calls are not aligned. For a project, use your IDE formatter; use this page for snippets, patches, log excerpts and code you cannot open in an editor.

Recent tools: