Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Tips for golfing in Java

+2
−0

This is a list of golfing tips for the language known as Java. If you have a tip, add it in!

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Don't use Java. (3 comments)

7 answers

+3
−0

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!

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Great tip! It's not just Strings, Java 9, 11, etc. added some methods to `Stream`s too (e.g. `takeWhi... (1 comment)
+2
−0

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 [] {}
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

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;}
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Use for(;;) instead of while(true)

The title's self-explanatory. A byte saver.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

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){}}

Try it online!

Used on "Hello, {name}!"

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

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");
}

Try it online!

With this, you can take out the following functions from java.util.Math:

  • Math.max() and Math.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);
	}
}

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

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 ints 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);
}

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!