☕ Java Formatter
Beautify messy Java code — supports Java 8 – 21, Spring Boot, Lombok, records and sealed classes.
Paste Java code on the left to see formatted output
Auto-formats as you type · 100% private · Java 8 – 21About Java Formatter
Consistent code formatting is essential for readability, code review efficiency, and long-term maintainability of Java projects. This browser-based Java formatter beautifies messy or minified Java code with zero server round-trips — paste your code and get cleanly indented, consistently spaced output in milliseconds. It supports Java 8 through 21, covering modern language features like records, sealed classes, text blocks, switch expressions with yield, and pattern matching instanceof.
Whether you are formatting a full Spring Boot service class, a Lombok-annotated POJO, a stream pipeline, or a snippet pasted from Stack Overflow, this tool applies the Google Java Style Guide conventions by default — with options to switch between K&R and Allman brace styles, adjust line wrap length, and sort import groups. All processing runs in your browser; your code never leaves your device.
How to format Java code
- Paste your Java code into the left panel — a full class file, a method, a code block, or a snippet.
- Select the code type (Full Class, Method/Field, Code Block, or Expression) to help the formatter parse ambiguous input correctly.
- Choose your preferred brace style — K&R (opening brace on the same line, Java convention) or Allman (opening brace on a new line).
- Set the indent size (2 or 4 spaces, or Tab) and line wrap length.
- Enable Sort Imports to group and alphabetise import statements by package.
- Copy the formatted output with the ⎘ Copy button.
Features
📄 Full Class / File
Format complete .java files: package declaration, import groups, class body, inner classes, enums, and annotations.
🔧 Method / Field
Format individual method declarations, field definitions, constructor signatures, and Lombok-annotated classes.
📦 Code Block
Format statement blocks, switch-case, try-catch-finally, for/while loops, and nested control structures.
⚡ Expressions
Format lambda expressions, method references, stream chains, ternary expressions, and record constructors.
🎯 K&R vs Allman
K&R keeps { on the same line (Java convention). Allman puts { on a new line, used in some enterprise codebases.
🔤 Sort Imports
Groups imports: static first, then java.*, javax.*, org.*, com.*, and others — each group alphabetically sorted.
📏 Line Wrap
Wraps long method signatures, chained calls, and string concatenations. Adjustable from 60 to 180 characters per line.
🌟 Java 21 Support
Records, sealed classes with permits, pattern matching instanceof, switch expressions with yield, and text blocks.
Frequently Asked Questions
What Java versions does this formatter support?
Java 8 through 21. This includes Java 8 lambdas and streams, Java 14+ records, Java 17 sealed classes and pattern matching, Java 15+ text blocks, and Java 21 pattern matching in switch expressions. If you are using Java 22+ features, formatting may still work for most code but edge cases with very new syntax may not be handled perfectly.
What is the difference between K&R and Allman brace styles?
In K&R style (the Java convention and Google Java Style Guide default), opening braces appear at the end of the statement line: public void method() {. In Allman style, opening braces appear on their own line: public void method() followed by { on the next line. Both styles produce valid Java — the choice is a team convention.
Does this formatter sort imports correctly for Spring Boot projects?
Yes. Import sorting groups imports as: static imports first, then java.*, javax.*, jakarta.*, org.springframework.*, org.*, com.*, and other packages. Each group is sorted alphabetically with a blank line between groups. This matches the convention used by IntelliJ IDEA and the Google Java Style Guide. When Sort imports is off, your original import order is preserved exactly.
Can I format Java code that uses Lombok annotations?
Yes. Lombok annotations like @Data, @Builder, @AllArgsConstructor, @NoArgsConstructor, @Getter, @Setter, @Slf4j, and others are treated as standard annotations and formatted accordingly. The formatter does not process Lombok's generated code — it formats the source as written.
Why does my formatted code look different from IntelliJ's auto-format?
This tool parses your code with a real Java parser (Prettier's Java plugin) running entirely in your browser, falling back to a fast pattern-based engine for fragments the parser cannot handle. For the vast majority of code the output matches IDE formatters closely, but individual wrapping decisions on very long expressions may differ slightly from IntelliJ's or Eclipse's own algorithms. The Google Java Style preset uses 2-space indent and 100-column lines; Java Standard uses 4-space indent and 120 columns.