Java Code Formatter (Google and AOSP Style)
Paste or open .java files, choose Google or AOSP style, and see every change in a side-by-side diff before you apply it or download the result.
Runs in your browser. Your files never leave your device. See how to check
…
How to do it
- 1Add your code
Paste into the source box, open .java files, or load the samples.
- 2Pick a style
Choose the Google or AOSP preset, then adjust indent, braces or import order.
- 3Read the diff
Compare the original and formatted lines side by side, or as a unified diff.
- 4Fix the issues
Open the issues tab and jump to unclosed brackets, missing braces and long lines.
- 5Apply or undo
Apply the result to one file or all files; undo with Ctrl+Z if needed.
- 6Download
Download the formatted file, a ZIP of every file, or a .patch for your repository.
About this tool
This Java formatter re-prints source code in the two house styles most Java teams follow. Google style uses a two-space block indent, a four-space continuation indent and a 100-column limit, and it keeps every static import in one block above the regular imports. AOSP style, used by the Android Open Source Project, indents blocks by four spaces and continuations by eight, and splits imports into groups: static imports, then android packages, then third-party packages such as com, junit, net and org, and finally java and javax. The engine is written in TypeScript and runs entirely in your browser. It first breaks the file into tokens, so string literals, char literals, text blocks and comments are recognised as single units and are never altered. It then rebuilds each line with the indentation implied by braces, parentheses and switch labels, normalises the spaces around operators, commas, keywords and lambda arrows, tells generic angle brackets apart from less-than comparisons, moves braces to match the chosen style and joins "} else {" onto one line. Import lines are sorted and deduplicated, and imports whose class name never appears in the file can be removed, which is what google-java-format does by default. You can open many files at once, switch between them, and download a ZIP of the whole batch.
What the checks report
Besides formatting, the tool reports problems that a formatter alone would not fix. Every bracket is matched, so a missing closing brace is reported with the line and column where the unclosed bracket was opened. A public top-level class whose name differs from the file name is flagged, because javac rejects it. Bodies of if, for and while without braces are listed, since both Google and AOSP style require braces even around a single statement. Lines longer than the column limit are listed with their length, and wildcard imports are counted.
Worked example
The first sample, OrderService.java, has 47 lines with braces on their own lines, missing spaces around operators, three blank lines in a row and an unused import of java.util.Map. With the Google preset the output has 43 lines: the diff shows 21 changed lines, 7 added and 11 removed. The static import of requireNonNull moves to its own block at the top, the Map import is removed and reported as unused, and the three remaining imports are sorted. The class brace joins the class line, "public OrderService( ) { }" becomes "public OrderService() {}", the else branch becomes "} else {", and the case labels sit two spaces inside the switch with their statements. The text block that builds the report keeps its exact content. Switching to AOSP gives 44 lines with four-space indentation and the Order import in a separate third-party group.
Limits
The built-in engine keeps your line breaks: it re-indents and re-spaces lines but does not wrap a long expression or join short ones, so long lines are only reported. Press the official formatter button if you need wrapping; clang-format or Prettier's Java plugin then loads from jsDelivr and runs locally. Unused-import detection compares simple names, so an import used only inside a string is treated as unused. The tool does not compile your code or check types.
Worked example
OrderService.java with the Google preset: 47 lines become 43, 21 changed, unused java.util.Map import removed, static import in its own block.
Quick facts
- Category: Developer & Data
- Works in your browser, no sign-up, no upload.
- Your input stays on your device.
- Available in English.
- Content last reviewed:
- Official source: Google Java Style Guide
Frequently asked questions
What is the difference between Google and AOSP Java style?
Google style indents blocks by 2 spaces and continuation lines by 4, and keeps all non-static imports in one sorted block. AOSP indents by 4 and 8 spaces and splits imports into android, third-party and java groups.
Will the formatter change my strings or comments?
No. String and char literals, text blocks and line comments are copied byte for byte. For a block comment, only the leading spaces of its later lines move when the comment is re-indented.
Does it remove unused imports?
Yes, when the option is on. A single-type import is removed if its class name appears nowhere else in the file, including Javadoc links. Static and wildcard imports are never removed.
Can I format several files at once?
Yes. Open as many .java files as you like, check each one in the diff, then use Apply to all files or download every formatted file in one ZIP.
Why are long lines not wrapped?
Wrapping needs a full parser and line-breaking rules, which the built-in engine does not have. Long lines are listed in the issues tab, and the optional clang-format or Prettier Java build can wrap them.
Is my code uploaded anywhere?
No. The built-in formatter runs in this page. Even the optional official formatters only download their own code from jsDelivr; your source stays on the device.
