Java Formatter
Paste Java code and get it back consistently indented, spaced and wrapped. Choose indent size, brace style and line width; the instant built-in formatter runs as you type, and clang-format compiled to WebAssembly is one click away. Nothing is uploaded.
Last updated:
Tip: press Ctrl + Enter to format; Tab indents inside the editor (press Esc first to move focus). Your code is formatted in your browser and is never uploaded, compiled or run.
How to use the Java Formatter
- Paste your Java code into the left editor, open a .java file, or load one of the samples.
- Pick indentation, brace style and line width; the defaults (4 spaces, attached braces, 100 columns) follow common Java conventions.
- Read the result on the right: it updates as you type, or press Format Java (Ctrl + Enter).
- Fix any syntax problem shown in red: click Show in input to jump to the unclosed brace or string.
- Copy or download the formatted code as a .java file.
Java formatting conventions at a glance
Most Java teams follow either the Google Java Style Guide or the older Oracle (Sun) Code Conventions. They agree on the essentials and differ mainly in indent size and line length:
| Rule | Google Java Style | Oracle / IDE defaults | Set it here with |
|---|---|---|---|
| Block indentation | 2 spaces | 4 spaces | Indentation |
| Continuation indent | +4 spaces | +8 spaces | Automatic (twice the indent) |
| Column limit | 100 | 80 (Oracle), 120 (IntelliJ) | Line width |
| Opening brace | End of line (K&R) | End of line | Brace style: Attach |
else, catch, finally | Same line as } | Same line as } | Attach (or Stroustrup for a new line) |
| Line breaks in long expressions | Before operators and before . | After commas, before operators | Automatic |
case labels | Indented inside switch | Same column as switch (Oracle) | Indented |
What the formatter changes
- Spacing: one space around binary and assignment operators, after commas and keywords such as
if,for,while,catchandsynchronized; no space inside parentheses or before a method call's(. Generics stay tight:Map<String, List<Integer>>. - Casts: a space after the cast, as Google style requires:
(int) value. - Line breaks: one statement per line, braces placed by the chosen style, and long lines wrapped after commas, before
&&/||or before chained calls such as.stream().map(…). Line breaks you put before.in a stream pipeline are kept. - Blank lines: runs of blank lines are reduced to one, and blank lines right after
{or before}are removed. - Kept exactly: comments, Javadoc text (re-aligned to the new indent), string and character literals, text blocks and annotations.
The formatter does not reorder imports or members, rename anything or add missing braces. To compare the result with your original, paste both into the Code Diff Checker. Generating model classes from an API response? Try JSON to Java Class.
Frequently asked questions
Does this Java formatter change what my code does?
No. It only changes whitespace: indentation, spaces around operators, line breaks and brace placement. Tokens, names, strings, comments and text blocks are kept exactly, so the compiled program is the same.
What is the difference between the built-in engine and clang-format?
The built-in engine is a small token-based formatter that runs instantly and works offline. clang-format is the LLVM formatter that also supports Java; it is loaded once as WebAssembly (about 2 MB) and produces output close to what you get from the command line with BasedOnStyle: Google.
Should Java use 2 or 4 spaces?
Both are common. The Google Java Style Guide uses 2 spaces with a 100-column limit, while the older Oracle (Sun) code conventions and most IDE defaults (IntelliJ IDEA, Eclipse) use 4 spaces. Pick the one your project already uses.
Which brace style is standard for Java?
Attached braces (K&R style, opening brace at the end of the line) are the convention in nearly all Java code, including the Oracle conventions and Google style. Allman style, with braces on their own lines, is valid Java but unusual.
Can it format code that does not compile?
Yes, as long as brackets and strings are balanced. The formatter does not type-check, so missing imports or unknown classes are fine. If a brace, bracket or string is left open, it reports the line and column and still formats on a best-effort basis.
Does it support modern Java syntax?
Yes: generics and wildcards, lambdas, method references (::), records, sealed interfaces, switch expressions with arrow cases and yield, pattern matching instanceof, var and text blocks (triple-quoted strings) are recognized.
Is my source code uploaded?
No. Formatting happens entirely in your browser. Only the optional clang-format engine is downloaded from a public CDN; your code is never sent anywhere.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.