Home › Developer Tools › Java Formatter

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.

Advertisement
Advertisement

How to use the Java Formatter

  1. Paste your Java code into the left editor, open a .java file, or load one of the samples.
  2. Pick indentation, brace style and line width; the defaults (4 spaces, attached braces, 100 columns) follow common Java conventions.
  3. Read the result on the right: it updates as you type, or press Format Java (Ctrl + Enter).
  4. Fix any syntax problem shown in red: click Show in input to jump to the unclosed brace or string.
  5. 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:

RuleGoogle Java StyleOracle / IDE defaultsSet it here with
Block indentation2 spaces4 spacesIndentation
Continuation indent+4 spaces+8 spacesAutomatic (twice the indent)
Column limit10080 (Oracle), 120 (IntelliJ)Line width
Opening braceEnd of line (K&R)End of lineBrace style: Attach
else, catch, finallySame line as }Same line as }Attach (or Stroustrup for a new line)
Line breaks in long expressionsBefore operators and before .After commas, before operatorsAutomatic
case labelsIndented inside switchSame 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, catch and synchronized; 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.

Related tools

C# FormatterBeautify C# code online: Allman or K&R braces, indent size and line width, clang-format option.JSON to Java ClassTurn JSON into Java POJOs or records with Lombok, getters/setters and Jackson or Gson annotations.Code Diff CheckerCompare two code files side by side or unified, with syntax colors and changes highlighted.TypeScript FormatterFormat TypeScript and TSX code with Prettier: width, tabs, semicolons and quotes options.JSON FormatterBeautify, minify and validate JSON, with a collapsible tree view.SQL Formatter & BeautifierFormat, beautify or minify SQL queries for MySQL, PostgreSQL and more.JavaScript BeautifierFormat and beautify messy or minified JavaScript, or minify it.HTML FormatterBeautify and indent HTML code with a live preview.CSS FormatterBeautify CSS with indent controls and optional property sorting.XML FormatterBeautify, validate and minify XML with a collapsible tree view.ZIP File CreatorCompress files and folders into a ZIP.YAML Validator & FormatterValidate, format and minify YAML, and convert YAML to JSON or back.HTML MinifierMinify HTML: strip comments and whitespace safely and see how many bytes you save.CSS MinifierMinify CSS with csso or a built-in engine and see the size saved instantly.JavaScript MinifierMinify JavaScript with Terser: compress, mangle and see how many bytes you save.JSON MinifierMinify and validate JSON, sort keys and see exactly how many bytes you save.IPv6 Expander & CompressorExpand IPv6 addresses to full form or compress them per RFC 5952, one or many at once.GraphQL FormatterPretty-print and indent GraphQL queries, mutations and schemas, or minify them, instantly.CSS ValidatorValidate CSS: syntax errors, unknown properties and invalid values by line, plus auto-fix.Gzip Compress / DecompressGzip or deflate text and files, get Base64 output, and decompress Base64 or .gz files.IBAN ValidatorCheck an IBAN's mod-97 checksum, country and length, and see its bank code and account parts.JavaScript Syntax CheckerCheck JavaScript for syntax errors with line and column, for any ES version, script or module.JSON Schema ValidatorValidate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with clickable error paths.Markdown FormatterBeautify Markdown with Prettier: list markers, prose wrap, aligned tables and live preview.PHP FormatterBeautify PHP code with Prettier PHP: PSR-12 style, indent and brace options, syntax errors.XML MinifierMinify and validate XML: strip whitespace and comments, keep CDATA, see the size savings.HTML ValidatorCheck HTML for errors and warnings with line numbers, locally or with the W3C Nu checker.JavaScript ObfuscatorObfuscate JavaScript with string encoding, renaming and control-flow flattening presets.Phone Number FormatterFormat and validate lists of phone numbers to E.164, international or national style.Python FormatterFormat Python code in Black/Ruff style: line length, quote style, indent, copy or download.Responsive Design CheckerPreview any website at phone, tablet, laptop and desktop sizes side by side, with rotate and zoom.Browser Compatibility CheckerSearch CSS, JavaScript and HTML features and see which browser versions support them.Core Web Vitals CheckerCheck real-user LCP, INP and CLS for any URL or site and see if it passes Core Web Vitals.Heading Structure CheckerPaste HTML to see its H1-H6 outline and catch missing H1s, multiple H1s and skipped levels.Website Hosting CheckerFind out who hosts a website: hosting provider, IP addresses, ASN, network owner and location.Routing Number ValidatorCheck a US ABA routing number's checksum and format and see its Federal Reserve district.Spam Word CheckerScan an email subject and body for spam trigger words, excess caps, exclamation marks and risky links.Check Digit CalculatorCalculate or validate check digits for UPC, EAN, GTIN, SSCC, ISBN, ISSN and mod 11 codes.XSD ValidatorValidate XML against an XSD schema in your browser and see each error with its line number.Strftime Format BuilderBuild Python and C strftime format strings by clicking tokens, with a live preview and reference.Luhn Algorithm CheckerValidate numbers with the Luhn mod 10 check or calculate the missing check digit, step by step.
See all 41 Code Formatters & Validators →