Wiki: Java

Boilerplate

The shortest Java boilerplate (valid program) is

interface I{static void main(String[]a){}}

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='田';

Loop n times

Compare these two ways of looping n times. The latter is 2 bytes shorter.

interface I{static void main(String[]s){for(int i=0;i++<n;)f();}}
// vs
interface I{static void main(String[]s){for(I m:new I[n])f();}}