Tips for golfing in Java
This is a list of golfing tips for the language known as Java. If you have a tip, add it in!
Use higher versions of Java …
3y ago
Use `interface` The special …
3y ago
Abuse anonymous classes War …
3y ago
Abuse the C-like array syntax …
3y ago
Use `for(;;)` instead of `whil …
3y ago
A clever way to replace `Math` …
3y ago
Booleans are replaceable Ja …
3y ago
7 answers
A clever way to replace Math
functions
You can shorten if-else
statements through the a?b:c
syntax, where a
is the statement of if
, b
is the result of if
, and c
is the result of else
.
static void main(String[]a){
int i = 95;
System.out.print(i>0?"positive":"negative/zero");
}
With this, you can take out the following functions from java.util.Math
:
-
Math.max()
andMath.min()
import java.util.*;
interface M{
static void main(String[]x){
int a = 20, b = 30;
System.out.println(Math.max(a,b));
System.out.println(a>b?a:b);
System.out.println(Math.min(a,b));
System.out.println(a<b?a:b);
}
}
0 comment threads
Use higher versions of Java
Seriously, it's crazy what sort of features have been added since Java 8. I hardly recognize the language anymore...
Examples:
Constructing Lists
Before, you had to do something like Arrays.asList(new String[]{"a", "b", "c"})
, but in Java 9+ you have List.of("a", "b", "c")
, some huge savings.
var
keyword
Self explanatory - since Java 10, instead of ReallyLongTypeName a = x();
, you now can just do var a = x();
.
Strings
In Java 11, some cool stuff for Strings was added that could potentially be useful - for example, .lines()
returns the String separated by newlines as a Stream (saving over .split("\n")
).
Fancy switch statements
Before:
switch(thing){case A:return "a";case B:return "b";default:return "x";}
Java 13+:
return switch(thing){case A->"a";case B->"b";default->"x";}
You can even have blocks of statements in any of the cases using {}
.
There are many other new features that I did not think are relavant to golf, and many many others that I'm sure I missed - there is just so much stuff to unpack. Please contribute if you find something I missed!
1 comment thread
Use interface
The special thing about this is that it lets you drop public
when you originally call a class
to run the main
function.
class F{public static void main(String[]a{}}
interface M{static void main(String[]a){}}
Used on "Hello, {name}!"
0 comment threads
Abuse the C-like array syntax
If you ever have to declare two variables, one of type X
and the other of type X[]
, instead of two statements, you can declare them together using this atrocious syntax:
int i,a[],m[][];
This defines an int i
, an int[] a
, and an int[][] m
.
Fun fact
This also works with method return types, and can actually be useful if you want to apply annotations to the type an array holds, e.g.
public @NotNull String foo() @Nullable [] {}
0 comment threads
Abuse anonymous classes
Warning: This only works in very specific circumstances, and I've never actually needed it.
Instead of defining a method outside of your method, you can make an instance of an anonymous class to act as a closure. This'll only really help if what the method is closing over has a very long type or name. Here's an extremely contrived example where an extra method is defined because recursion is needed.
int g(List<Integer>b){return new Object(){int f(int x){return x>b.get(0)?f(x-1)*2:1;}}.f(3);}
The one above is 93 bytes, the one below is 97.
int g(List<Integer>b){return f(b,3);}int f(List<Integer>b,int x){return x>b.get(0)?f(b,x-1)*2:1;}
0 comment threads
Use for(;;)
instead of while(true)
The title's self-explanatory. A byte saver.
0 comment threads
Booleans are replaceable
Java, like Python, has pretty convenient yet nasty ways to golfing down code through comparison operators. Especially this case, this time with booleans. You can compare int
s and it will result to either false
or true
, which helps you break 1 and 2 bytes respectively.
static void main(String[]a){
System.out.println(1<0);
System.out.println(1>0);
}
1 comment thread