Java indentation
Rewrites nested blocks with even indentation so classes, methods, and loops line up. Choose 2 or 4 spaces per level to match your project.
class A{void run(){int x=1;}}Afterclass A {
void run() {
int x = 1;
}
}Paste messy or minified Java and get it back with even indentation, clear brace placement, and consistent spacing. Everything runs in your browser, so your code stays on your device.
Format uses clang-format. The style and indent options apply to Format.
Java can end up with uneven spacing after edits, merges, or a quick copy and paste. A formatter rewrites the code with even indentation, one statement per line, and consistent spacing, so the structure is clear at a glance. It keeps your code working the same and only changes the layout. This tool formats your code with clang-format, which runs fully in your browser.
Rewrites nested blocks with even indentation so classes, methods, and loops line up. Choose 2 or 4 spaces per level to match your project.
class A{void run(){int x=1;}}Afterclass A {
void run() {
int x = 1;
}
}Puts braces where the chosen style expects them and gives each statement its own line, so the structure of the code is clear at a glance.
if(ok){go();}else{stop();}Afterif (ok) {
go();
} else {
stop();
}Adds spaces around operators, after commas, and after keywords so the code reads cleanly. It never changes what the code does.
int sum=a+b,diff=a-b;Afterint sum = a + b, diff = a - b;Pick a base style like Google, LLVM, Chromium, Mozilla, WebKit, or Microsoft. Google is the common default for Java code.
void m( int a ,int b ){}Aftervoid m(int a, int b) {}A formatter helps any time code is hard to read. These are the cases that come up most:
This tool formats Java with clang-format, using the style you pick (Google by default). You can run clang-format locally for the same result:
| To | Use |
|---|---|
| Format a file in place | clang-format -i --style=Google Main.java |
| Save the style for a project | clang-format --style=Google --dump-config > .clang-format |
| Format on save | Most editors, including VS Code and IntelliJ with a plugin, can run clang-format on save |
Many Java teams use google-java-format instead, which is a separate tool with its own fixed rules. It produces similar Google-style code, but not always identical line breaks, so pick one and use it everywhere.
Java and JavaScript are different languages. If your code is JavaScript, use the JavaScript formatter instead. These formatters also run right in your browser: