Boilerplate
Since Java 25, it's not necessary to wrap main
in a class, so shortest boilerplate is:
void main(String[]a){}
// If the hole doesn't have arguments:
void main(){}
The var
data type
Defining a type with var
is often shorter. Note that multiple assignment is not possible with this method.
String s="foo";
// vs
var s="foo";
Unicode constants
This can save 1 to 2 characters on char scoring. If you need a large integer constant between 1000
and 65535
(inclusive), it may help to use a char literal instead of a number:
var n=30000;
// vs
var n='田';
Scientific Notation
This can save a few chars when using large numbers that end in zeros
var n=30000;
// vs
var n=3e4;
Use Fields to Initialize to Zero
If you need a single integer initialized to 0, using a field saves 1 byte:
void main(){for(int i=0;i<10;)IO.println(i++);}
// vs
int i;void main(){for(;i<10;)IO.println(i++);}
Java doesn't allow declaring multiple fields like this at once without explicit initializers.