Are there any useful shortcuts that can be used in Java?

As shown below, import already adds at least 17 characters to a program.

import java.io.*;

I understand that the simple solution would be to use another language, but it seems to be a real challenge to shorten Java programs.


Tips should be specific to Java: if they're applicable to most C-like languages, they belong in the more general list of tips.

  • 7
    package can be skipped. – st0le Jul 19 '12 at 7:08
  • In an answer, can't I just omit the imports assuming they are there? – Fabricio May 8 '14 at 15:24
  • 1
    @Fabricio Not unless the OP specifies so. – nyuszika7h Sep 26 '14 at 18:49
  • 27
    Best tip about golfing Java: don't use it. ;) – kirbyfan64sos Sep 25 '15 at 19:10
  • 2
    "I want to golf in java" good luck – sagiksp May 4 '17 at 7:11

38 Answers 38

  • Use the most recent possible java. Java 8 lets you use lambda expressions, so use it if you need anything even like functional objects.

  • Define shortened functions for things you use a lot. For instance, you have a hundred calls to exampleClassInstance.doSomething(someParameter), define a new function void d(ParameterType p){exampleClassInstance.doSomething(p)} and use it to save yourself some characters.

  • If you are using a particular long class name more than once, like

    MyEventHandlerProxyQueueExecutorServiceCollectionAccessManagerFactory
    

    instead define a new class:

    class X extends MyEventHandlerProxyQueueExecutorServiceCollectionAccessManagerFactory{}
    

    If you are only using one particular method of that class (but still need to instantiate it), you can define a shortened version inside the new class at the same time.

  • Use function type parameters to shorten things, where possible, like this:

    <T>void p(T o){System.out.println(o);}
    
  • Use for(;;) instead of while(true).

  • Do not use access modifiers unless absolutely necessary.

  • Do not use final for anything.

  • Never put a block after a for loop (but a foreach loop for(x:y) is different). Additional statements should be placed inside the for statement itself, like for(int i=0;i<m;a(i),b(++i))c(i);.

  • Use inline assignment, incrementation, instantiation. Use anonymous inline classes where appropriate. Use lambdas instead if possible. Nest function calls. Some functions are guaranteed to return their parent object, these ones are actually even meant to be chained together.

  • Your main method throws Exceptions, not catches them.

  • Error is shorter than Exception. If for some reason you really need to throw messages up the stack, use an Error, even if it is perfectly normal situation.

  • If some condition would require immediate termination, use int a=1/0; rather than throw null; or System.exit(0);. At run time, this throws an ArithmeticException. If you already have a numeric variable in your code, use it instead. (If you already have import static java.lang.System.*;, go with exit(0);.)

  • Instead of implementing interfaces, like List<E>, extend an immediate (or not-so-immediate, if there is any advantage to doing so at all) child class, like AbstractList<E>, which provides default implementations of most of the methods, and requires only the implementation of a few key pieces.

  • Write your code out in longhand first, with newlines, indentation, and full variable names. Once you have working code, then you can shorten names, move declarations around, and add shortcut methods. By writing it out long to start, you give yourself more opportunity to simplify the program as a whole.

  • Compare alternative optimizations to a piece of code, because the most optimal strategy can change dramatically with very small changes to the code. For instance:

    • If you have only up to two calls to Arrays.sort(a), the most efficient way to is to call it with its fully qualified name, java.util.Arrays.sort(a).
    • With three or more calls, it is more efficient to instead add a shortcut method void s(int[]a){java.util.Arrays.sort(a);}. This should still use the fully-qualified name in this case. (If you need more than one overload, you are probably doing it wrong.)
    • However, if your code needs to also copy an array at some point (usually done with a short for loop in golfing, in the absence of an easily-accessible library method), you can take advantage of Arrays.copyOf to do the task. When more than one method is used, and there are 3 or more calls, doing import static java.util.Arrays.*; is the most efficient way of referring to those methods. After that, only if you have more than 8 separate calls to sort should you be using a shortcut method for it, and only at 5 or more calls is a shortcut warranted for copyOf.

    The only real way of performing such analysis on code is to actually perform potential modifications on copies of the code, and then compare the results.

  • Avoid using someTypeValue.toString(); method, instead just append someTypeValue+"".

  • If you do need windows, don't use Swing, use AWT (unless you really need something from Swing). Compare import javax.swing.*; and import java.awt.*;. Additionally, components in Swing have a J prepended to their name (JFrame, JLabel, etc), but components in AWT don't (Frame, Label, etc)

Use interface instead of class.

In java 8, static methods were added to interfaces. In interfaces, all methods are public by default. Consequently

class A{public static void main(String[]a){}}

can now be shortened to

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

which is obviously shorter.

For example, I used this feature in the Hello, World! challenge.

With varargs you can "cast" a parameter to an array of the same type:

void f(String...x){
    x=x[0].split("someregex");
    // some code using the array
}

instead of

void f(String s){
    String[]x=s.split("someregex");
    // some code using the array
}

With a static import:

import static java.lang.System.out;
// even shorter (thanks to Johannes Kuhn):
import static java.lang.System.*;

you can save some boilerplate later, but you need multiple invocations to reach a payoff:

public static void main (String[] args) {
    out.println ("foo");    
    out.println ("bar");    
    out.println ("baz");    
}
  • 7
    :O. You can do this?! And all this time I thought that this was impossible in Java! – Justin Dec 20 '13 at 7:17
  • 11
    you can even use import static java.lang.System.*. – Johannes Kuhn Dec 20 '13 at 19:16
  • I know this is an old answer, but in Java 10 you can now do var o=System.out; which only needs to be used twice before it pays off – Luke Stevens Nov 27 at 10:57
  • @LukeStevens: Well, maybe you find some other improvements, possible with Java10, and form a separate answer around Java10? – user unknown Nov 28 at 4:59

The argument to main doesn't have to be called args, and you can cut some whitespace:

public static void main(String[]a){}

will do just fine.

  • Do answers need to include the main function if it doesn't explicitly state to write a full program? I've been using lambda expressions as answers. – Makotosan Mar 22 at 13:14
  • 1
    @Makotosan No they don't; Lambdas are usually fine. – daniero Mar 23 at 18:17

If you are going to be using some method a lot, assign its resident class to a variable. For example, assign System.out to a variable:

java.io.PrintStream o=System.out;
//now I can call o.print() or o.println() to the same effect as System.out.println()

Also for Integer.parseInt():

Integer i=1;
i.parseInt("some string");

This will almost surely trigger an ide warning about "accessing static method from variable"

If you ever have to use the boolean expressions true or false, replace them with 1>0 and 1<0 respectively.

For example:

boolean found=false;
for(i=0; i<10; i++) if(a[i]==42) found=true;

This linear search example can be reduced to

boolean f=1<0;
for(i=0;i<10;)if(a[i++]==42)f=1>0;
  • 10
    If you're going to need a lot of true/false, just add boolean t=1>0,f=1<0;. Then instead of 1>0, use t and save two chars per use. Payoff over 1>0 method comes at 10 uses. – Geobits Mar 11 '14 at 2:17
  • 23
    @Geobits: boolean t=1>0,f=!t; - one char shorter! – bobbel Apr 11 '14 at 14:07
  • 5
    The example is not really good. In this case and many(!) others you can avoid using true/false directly anyway: f|=a[i++]==42; saves quite a lot. – Ingo Bürk Sep 27 '14 at 11:59
  • @IngoBürk True. When I was writing this I was mostly thinking about library functions that uses boolean, but since I couldn't come up with any examples at the time of writing (I don't usually code in Java) I just wrote a simple example. – ace Sep 30 '14 at 19:17
  • 1
    @Geobits not too familiar with java but could you just define 1 and use t and !t (again I dont know Java, just curious) – Albert Renshaw Oct 3 '15 at 7:47

This may seem obvious, but there are shorter options for some Math functions:

a=Math.max(b,c);
a=b>c?b:c;

a=Math.min(b,c);
a=b<c?b:c;

a=Math.abs(b);
a=b<0?-b:b;

a=Math.round(b);
a=(int)(b+.5);          // watch for precision loss if it matters

Rather than using the import static java.lang.System.* technique to save on println() statements, I've found that defining the following method is much more effective at saving characters:

static<T>void p(T p){
    System.out.println(p);
}

This is because it can be invoked as p(myString) rather than out.println(myString) which has a much quicker and more dramatic character payoff.

If you need Integer.MAX_VALUE (2147483647), use -1>>>1. Integer.MIN_VALUE (-2147483648) is better written 1<<31.

If you need to grab a number from an argument (or any other string), normally you see something like:

public static void main(String[]a){
    int n=Integer.valueOf(a[0]);
    ...
}

Many times, you don't need an Integer. Plenty of challenges don't use large numbers. Since Short and Byte will both unbox to an int, use the more appropriate valueOf() instead and save a couple bytes.

Keep your actual variable as an int, though, since it's shorter than both byte and short:

int n=Byte.valueOf(a[0]);

If you need to do this for multiple numbers, you can combine with this method:

Byte b=1;
int n=b.valueOf(a[0]),m=b.valueOf(a[1])...
  • 9
    int n=new Byte(a[0]); is three shorter. If the number might be larger, use long n=new Long(a[0]), it's still better than ints in most cases. – Ypnypn Nov 18 '14 at 0:55

Don't use public class. The main method needs to be public, but its class doesn't. This code works:

class S{public static void main(String[]a){System.out.println("works");}}

You may run java S even though class S is not a public class. (Update: I was using Java 7 when I wrote this tip. In Java 8, your main method should be in an interface. In Java 5 or 6, your main method should be in an enum.)

Plenty of Java programmers don't know this! About half the answers to a Stack Overflow question about main in non-public class wrongly claim that the main method must be in a public class. Now you know better. Delete the public in public class and save 7 characters.

  • 1
    Unless you are targeting a Java prior to 1.8, the construct interface s{static void main(String[]... is shorter. If you must have a compilable source file and main method. Because in a Java 1.8 interface, all methods are public so you can skip the modifier on the method(s). – Douglas Held Aug 22 '17 at 0:07
  • I have not recently used Java, so my answer is getting outdated. I forgot that interfaces can have methods in Java 8. – kernigh Sep 5 '17 at 1:16
  • I didn't learn it from programming; I learned it from golfing :) – Douglas Held Sep 5 '17 at 21:34

Some small code-golfing tips

These tips were a bit too small for a separated answers, so I will use this answer for very small codegolfing tips that I found or came up with, and aren't mentioned in the other tips yet:

Removing the last character of a String:

// I used to do something like this:
s.substring(0,s.length()-1)     // 27 bytes

// But this is shorter:
s.replaceAll(".$","")           // 21 bytes

In some cases you know what the last character is beforehand, and you also know this character only occurs once in the String. In that case you can use .split instead:

// As example: "100%" to "100"
s.split("%")[0]                 // 15 bytes

Encoding shortcuts:

// When you want to get the UTF-8 bytes I used to do this:
s.getBytes("UTF-8");     // 20 bytes

// But you can also use "UTF8" for the same result:
s.getBytes("UTF8");      // 19 bytes

All encodings have a canonical name used in the java.nio API, as well as a canonical name used in the java.io and java.lang APIs. Here is a full list of all supported encodings in Java. So always use the shortest of the two; the second is usually shorter (like UTF-8 vs utf8, Windows-1252 vs Cp1252, etc.), but not always (UTF-16BE vs UnicodeBigUnmarked).

Random boolean:

// You could do something like this:
new java.util.Random().nextBoolean()     // 36 bytes

// But as mentioned before in @Geobits' answer, Math.random() doesn't require an import:
Math.random()<.5                         // 16 bytes

Primes:

There are a lot of different ways to check for primes or get all primes, but @Geobits' answer here is the shortest. Here is a copy-paste as reference:

// Check if n is a prime:
Object p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n>1;}

// Which can easily be modified to loop through primes:
void c(){for(int n=2,i,x;;n++){for(x=n,i=2;i<x;x=x%i++<1?0:x);if(x>1)/*do something with the prime here*/;}}

NOTE: Usually you can merge it with other existing loops depending on how you want to use it, so you won't need a separate method. This saved a lot of bytes in this answer for example.

Integer truncation instead of Math.floor/Math.ceil:

If you are using positive doubles/floats and you want to floor them, don't use Math.floor but use an (int)-cast instead (since Java truncates on integers):

double d = 54.99;

int n=(int)Math.floor(d);     // 25 bytes

int m=(int)d;                 // 13 bytes

// Outputs 54 for both

The same trick can be applied to negative doubles/floats you want to ceil instead:

double d = -54.99;

int n=(int)Math.ceil(d);     // 24 bytes

int m=(int)d;                // 13 bytes

// Outputs -54 for both

Use &1 instead of %2 to get rid of parenthesis:

Because the Operator Precedence of & is lower than default arithmetic operators like */+- and %, you can get rid of parenthesis in some cases.

// So instead of this:
(i+j)%2     // 7 bytes

// Use this:
i+j&1       // 5 bytes

Note that this doesn't really help in boolean-checks, because then you'd still need parenthesis, they're just moved a bit:

(i+j)%2<1    // 9 bytes
(i+j&1)<1    // 9 bytes

BigIntegers and creating variables for static method calls:

When using BigIntegers, only create it once which you can then re-use. As you may know, BigInteger contains static fields for ZERO, ONE and TEN. So when you only use those three, you don't need an import but can use java.Math.BigInteger directly.

// So instead of this:
import java.math.BigInteger.*;
BigInteger a=BigInteger.ONE,b=BigInteger.ZERO;                // 76 bytes

// or this:
java.math.BigInteger a=java.math.BigInteger.ONE,b=a.ZERO;     // 57 bytes

// Use this:
java.math.BigInteger t=null,a=t.ONE,b=t.ZERO;                 // 45 bytes                  

NOTE: You have to use =null so t is initialized in order to use t..

Sometimes you can add multiple BigIntegers to create another to save bytes. So let's say you want to have the BigIntegers 1,10,12 for some reason:

// So instead of this:
BigInteger t=null,a=t.ONE,b=t.TEN,c=new BigInteger(12);     // 55 bytes

// Use this:
BigInteger t=null,a=t.ONE,b=t.TEN,c=b.add(a).add(a);        // 52 bytes

As correctly pointed out in the comments, the trick with BigInteger t=null; for it's static method calls can also be used with other classes.
For example, this answer from 2011 can be golfed:

// 173 bytes:
import java.util.*;class g{public static void main(String[]p){String[]a=p[0].split(""),b=p[1].split("");Arrays.sort(a);Arrays.sort(b);System.out.print(Arrays.equals(a,b));}}

// 163 bytes
class g{public static void main(String[]p){java.util.Arrays x=null;String[]a=p[0].split(""),b=p[1].split("");x.sort(a);x.sort(b);System.out.print(x.equals(a,b));}}

getBytes() instead of toCharArray()

When you want to loop over the characters of a String, you'll usually do this:

for(char c:s.toCharArray())    // 27 bytes
// or this:
for(String c:s.split(""))      // 25 bytes

Looping over the characters can be useful when printing them, or appending them to a String, or something similar.

However, if you only use the chars for some unicode-number calculations, you can replace the char with int, AND you can replace toCharArray() with getBytes():

for(int c:s.getBytes())        // 23 bytes

Or even shorter in Java 8+:

s.chars().forEach(c->...)      // 22 bytes

In Java 10+ looping over the character to print can now also be done in 22 bytes:

for(var c:s.split(""))         // 22 bytes

Random item from a List:

List l=...;

// When we have an `import java.util.*;` in our code, shuffling is shortest:
return l.get(new Random().nextInt(l.size()));     // 45 bytes
return l.get((int)(Math.random()*l.size()));      // 44 bytes
Collections.shuffle(l);return l.get(0);           // 39 bytes

// When we don't have an `import java.util.*` in our code, `Math.random` is shortest:
return l.get(new java.util.Random().nextInt(l.size()));     // 55 bytes
return l.get((int)(Math.random()*l.size()));                // 44 bytes
java.util.Collections.shuffle(l);return l.get(0);           // 49 bytes

Check if a String contains leading/trailing spaces

String s=...;

// I used to use a regex like this:
s.matches(" .*|.* ")     // 20 bytes
// But this is shorter:
!s.trim().equals(s)      // 19 bytes
// And this is even shorter due to a nice feature of String#trim:
s!=s.trim()              // 11 bytes

Why does this work, when != on Strings is to check for reference instead of value in Java? Because String#trim will return "A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space." I've used this, after someone suggested this to me, in this answer of mine.

Palindrome:

To check if a String is a palindrome (keeping in mind both even and odd lengths of Strings), this is the shortest (.contains works here because we know both the String itself and its reversed form are of equal length):

String s=...;
s.contains(new StringBuffer(s).reverse())    // 41 bytes

.contains(...) instead of .equals(...+"") thanks to @assylias's comment here.

Either is 0, or both are 0?

I think most already know this one: if you want to check if either a or b is zero, multiply instead to save bytes:

a==0|b==0    // 9 bytes
a*b==0       // 6 bytes

And if you want to check if both a and b are zero, you could use a bitwise-OR, or add them together if they are always positive:

a==0&b==0    // 9 bytes
(a|b)==0     // 8 bytes (if either `a`, `b` or both can be negative)
a+b<1        // 5 bytes (this only works if neither `a` nor `b` can be negative)

Even = 1, odd = -1; or vice-versa

// even = 1; odd = -1:
n%2<1?1:-1        // 10 bytes
1-n%2*2           // 7 bytes

// even = -1; odd = 1:
n%2<1?-1:1        // 10 bytes
n%2*2-1           // 7 bytes

The reason I add this was after seeing k+(k%2<1?1:-1) in this answer:

k+(k%2<1?1:-1)    // 14 bytes

// This would already have been shorter:
k%2<1?k+1:k-1     // 13 bytes

// But it can also be:
k%2*-2-~k         // 9 bytes

Loop n times in Full Program

If we have a challenge where a full program is mandatory, and we need to loop a specific amount of times, we can do the following:

// instead of:
interface M{static void main(String[]a){for(int n=50;n-->0;)/*do something*/}}  // 78 bytes
// we could do:
interface M{static void main(String[]a){for(M m:new M[50])/*do something*/}}    // 76 bytes

The same applies when we have to take this range as input:

interface M{static void main(String[]a){for(int n=new Byte(a[0]);n-->0;)/*do something*/}}  // 90 bytes
interface M{static void main(String[]a){for(M m:new M[new Byte(a[0])])/*do something*/}}    // 88 bytes

Credit to @JackAmmo in this comment.

try-finally instead of try-catch(Exception e) when returning, and when to use it

If you can't use a throws Exception but have to catch and do something with it before returning, you can use finally instead:

try{...}catch(Exception e){return ...;}    // 33 bytes
try{...}finally{return ...;}               // 22 bytes

As for an example of when to use a try-catch, I can refer to this answer of mine (credit for the indirect golf goes to @KamilDrakari). In this challenge we have to loop diagonally over an NxM matrix, so we have to determine whether the amount of columns or amount of rows is the lowest as our maximum in the for-loop (which is quite expensive in terms of bytes: i<Math.min(a.length,a[0].length)). So, simply catching the ArrayIndexOutOfBoundsException using catch-finally is shorter than this check, and thus saves bytes:

int[] a = ...;

int r=0,i=0;for(;i<Math.min(a.length,a[0].length);)r=...i++...;return r;    // 66 bytes

int r=0,i=0;try{for(;;)r=...i++...;}finally{return r;}                      // 48 bytes

NOTE: This only worked because of the return r; in the finally. I've been suggested to modify the first cell, like @KamilDrakari did in his C# answer to save bytes. However, in Java this means I will have to change it to m->{try{for(int i=1;;m[0][0]=f(m[0][0],m[i][i++]));}catch(Exception e){}} (73 bytes), actually increasing the byte count instead of decreasing if I could have used finally.

Math.pow(2,n)

When you want a power of 2, a bit-wise approach is much shorter:

(int)Math.pow(2,n)    // 16 bytes
(1<<n)                // 6 bytes

Combining bit-wise and logical checks instead of using parenthesis

I think it is well-known by now that & and | can be used instead of && and || in Java (boolean) logical checks. In some cases you'd still want to use && instead of & to prevent errors though, like index >= 0 && array[index].doSomething. If the && would be changed to & here, it will still evaluate the part where it uses the index in the array, causing an ArrayIndexOutOfBoundsException, hence the use of && in this case instead of &.

So far the basics of &&/|| vs &/| in Java.

When you want to check (A or B) and C, the shortest might seem to use the bit-wise operators like this:

(A|B)&C    // 7 bytes

However, because the bit-wise operators have operator precedence over the logical checks, you can combine both to save a byte here:

A|B&&C     // 6 bytes

Use n+=...-n instead of (long)...

When you have a long as both in and output in a lambda, for example when using Math.pow, you can save a byte by using n+=...-n instead of (long)....
For example:

n->(long)Math.pow(10,n)    // 23 bytes
n->n+=Math.pow(10,n)-n     // 22 bytes

This saved a byte in this answer of mine, and even two bytes by combining -n-1 to +~n in this answer of mine.

  • More generally for your last point, you can access/invoke static members from a non-static context such as an instance of the object. – Poke Jun 16 '17 at 13:19
  • 1
    I do not understand your ceil tip. Why would you want to ceil positive integers? Also, I am not sure if the ceil implementation works. – Jonathan Frech Sep 21 '17 at 13:48
  • 1
    since Java automatically floors on integers; I think the proper term is truncation, not flooring. – Jonathan Frech Sep 21 '17 at 14:02
  • 1
    Another Palindrome strategy String t="";for(int i=s.length();--i>=0;t+=s.charAt(i));return s.equals(t); – Roberto Graham Oct 5 '17 at 11:05
  • 1
    @RobertoGraham I actually copied my original code from the wrong challenge.. Just s.equals(new StringBuffer(s).reverse()+"") is enough. – Kevin Cruijssen Oct 5 '17 at 11:16

For golfing that doesn't require input, you can use static blocks, and run it just fine without any main method, just compile it with Java 6.

public class StaticExample{
    static {
        //do stuff
    }
}
  • 1
    Did you try to compile and run it? This block is run when the class gets loaded by the class loader. But the class loader won't load anything until it knows of a class with a main method. – Cruncher Dec 20 '13 at 15:31
  • @Cruncher You can get around that by yourself telling java on the command line/in a manifest file which class to load. – AJMansfield Dec 22 '13 at 1:20
  • 6
    @Cruncher, this worked with Java 6. Java 7 changed the way it works. – Peter Taylor Dec 31 '13 at 18:36
  • 1
    Throws an exception at the end but it works! Even in Java 7 – Jop V. Jun 23 '14 at 16:00
  • 2
    @JopVernooij If you don't want to have an exception thrown to your face, you can system.exit(), but you'll waste characters, no golf challenge ever asks you to avoid exceptions ;) – Fabinout Jul 1 '14 at 8:57

We all know about the bitwise xor (^), but it is also a logical xor.

So (a||b)&&!(a&&b) simply becomes a^b.

Now we can use xor.

Additionally, the operators | and & also work, just remember that operator precedence changes.

  • 5
    As long as you remember precedence, you can use & and | also. It might be useful if your conditions are already in parentheses, or if you're already working with booleans. – Geobits Apr 16 '14 at 18:23
  • 1
    If you need a (much) lower precedence, you can use != instead of ^ for xor, and == for xnor – Cyoce Nov 29 '16 at 21:24

You don't have to use Character.toLowerCase(char c). Instead use (c|32). Instead of Character.toUpperCase(char c) use (c&~32). This only works with ASCII letters.

  • c|~32 would tend to result in -1... better to use c-32. – feersum Dec 22 '14 at 13:25
  • 5
    @feersum That would not work if you wanted to make an upper case letter upper case. – TheNumberOne Dec 22 '14 at 13:32

Don't use Random!

In general, if you need random numbers, Random is a horrible way to go about it*. Much better to use Math.random() instead. To use Random, you need to do this (let's say we need an int):

import java.util.*;
Random r=new Random();
a=r.nextInt(9);
b=r.nextInt(9);

Compare that to:

a=(int)(Math.random()*9);
b=(int)(Math.random()*9);

and:

int r(int m){return(int)(Math.random()*m);}
a=r(9);
b=r(9);

The first method takes 41+15n characters (n is number of calls). The second is 25n characters, and the third is 43+7n.

So, if you only need it once or twice, use the inline Math.random() method. For three or more calls, you'll save by using a function. Either one saves characters on the first use over Random.


If you're already using Math.random() for double, remember that at four uses, it's still a savings to pull it out into:

double r(){return Math.random();}

For 33 characters, you'll save 10 on each call to r()


Update

If you need an integer and want to save on casting, don't cast it! Java auto-casts if you do an operation instead of an assignment. Compare:

a=(int)(Math.random()*9);
a=9;a*=Math.random();

* Unless you have to seed the PRNG for predictable results. Then, I don't see much of a way around it.

  • 2
    Don't forget about Random#nextGaussian though. – Justin Apr 18 '14 at 20:37
  • @Quincunx True, doing the math to get a good normal distribution would lose you any savings you got. I'll just refer to that as the exception that proves the rule ;) – Geobits Apr 18 '14 at 21:47
  • Note that (int)(Math.random()*9) has a very small modulo bias, because Math.random() returns 253 possible values, and 253 is not a multiple of 9. The probability of each number is within 1/9 plus or minus 5/(9*2**53), an error so small, it is almost exactly 1/9. – kernigh Jun 20 '14 at 16:32
  • @kernigh Right, I was using 9 just as an example, it could be anything. I'm relatively sure that nextInt() (or any other Random method) has a small bias as well, just due to how Java's PRNG works. – Geobits Jun 20 '14 at 19:59
  • 1
    Something related for when you want a random boolean: instead of new java.util.Random().nextBoolean() you can use Math.random()<.5. – Kevin Cruijssen Oct 4 '16 at 14:06

Convert String to number

There are multiple ways to convert a String to an numeric value:

String s = "12";

ABC.parseABC:

Short.parseShort(s); // 20 bytes
Integer.parseInt(s); // 20 bytes
Long.parseLong(s);   // 18 bytes

ABC.valueOf:

Short.valueOf(s);    // 17 bytes
Integer.valueOf(s);  // 19 bytes
Long.valueOf(s);     // 16 bytes

ABC.decode:

// Note: does not work for numeric values with leading zeros,
// since these will be converted to octal numbers instead
Short.decode(s);     // 16 bytes
Integer.decode(s);   // 18 bytes
Long.decode(s);      // 15 bytes

new ABC:

new Short(s);        // 13 bytes
new Integer(s);      // 15 bytes
new Long(s);         // 12 bytes

So, for code-golfing, it's best to use the constructor when converting a String to a numeric value.

The same applies to Double; Float; and Byte.


This doesn't always apply when you can re-use an already present primitive as object.
As example, let's say we have the following code:

// NOTE: Pretty bad example, because changing the short to int would probably be shorter..
//       but it's just an example to get the point across

short f(short i,String s){
  short r=new Short(s);  // 21 bytes
  ... // Do something with both shorts
}

You can use .decode instead of the shorter constructor by re-using the parameter as object:

short f(Short i,String s){   // Note the short parameter has changed to Short here
  short r=i.decode(s);   // 20 bytes
  ... // Do something with both shorts
}

Shortening returning

You can shorten return statements of strings by a byte with:

return "something";

to

return"something";

And, if you happen to begin your return statement with a parenthesis, you can do the same thing with them:

return (1+taxRate)*value;

to

return(1+taxRate)*value;

I guess quotes are considered like parentheticals? I actually picked this up through AppleScript, funnily enough, and thought it might be worth mentioning.

  • 1
    Same applies to number signs, like return-n; instead of return -n; or return~n; instead of return ~n;. As well as single instead of double quotes: return'A'; – Kevin Cruijssen Sep 21 '17 at 14:09
  • 1
    Basically, works for anything which can't be part of an identifier (i.e. non-letter and non-digit). – Paŭlo Ebermann Feb 17 at 16:55

Don't be afraid to use scientific notation

If you are dealing with doubles, or floats, you can use scientific notation for numbers. So instead of writing double a=1000 you can change it to double a=1e3 to save 1 byte.

Try using int instead of boolean

In some cases I've found that it's shorter to return an integer value from a method that would normally return a boolean, similarly to what might be done in C programs.

Right off the bat int is 4 bytes shorter than boolean. Each time you write return 0 instead of return 1<0, you save an additional 2 bytes and the same for return 1 over return 1>0.

The pitfall here is that each time you want to use the return value directly as a boolean, it costs 2 bytes (if(p(n)) v. if(p(n)>0)). This can be made up for by use of boolean arithmetic. Given a contrived scenario where you want to write

void a(int[]t){t[0]+=p(n)?10:0;}

you can instead write

void a(int[]t){t[0]+=p(n)*10;}

in order to save 2 bytes.

  • 6
    I do this pretty often when golfing, but keep in mind that the general consensus is that 0 and 1 don't constitute false/true in Java (and the JLS doesn't consider them that way either). So if the golf is specifically asking for truthy/falsy, you need to booleanize it (and, unfortunately, make it a boolean function, throwing even more bytes at it). – Geobits Aug 4 '15 at 22:56
  • 2
    t[0]+=p(n):10?0; Is this even valid? – dorukayhan Jun 30 '16 at 1:30
  • @dorukayhan no, it is meant to be t[0]+=p(n)?10:0;. (I edited it.) – Paŭlo Ebermann Feb 18 at 13:46

I don't know if you would consider this 'pure' Java, but Processing allows you to create programs with little initial setup (completed automatically).

For console output, you can have something as simple as:

println("hi"); //done

for graphical output, a little more:

void setup() {
  size(640,480);
}
void draw() {
  fill(255,0,0); //color used to fill shapes
  rect(50,50,25,25); //25x25 pixel square at x=50,y=50
}
  • +1 Excellent resource! I'll be sure to play around with it. – Rob Jul 19 '12 at 19:07
  • Would it be alright if I added other people's answers to this one? Or does that defeat the purpose of a community wiki? – Rob Sep 16 '12 at 13:45
  • 2
    By the way, you do not even have to call size at all; it will default to a 100 by 100 pixel square. In most OSes, the frame around it will be about twice that large, with the square centered and the rest of the area filled with content taken from the desktop. – AJMansfield Dec 20 '13 at 12:32
  • For graphical output, if you don't need animation, you can just write everything outside of setup() and draw() to use "static mode". You can also use 6-digit hex colors and the interpreter will change them, which sometimes pays off (#FF8000 < 255,128,0), and if you're using greyscale only one number needs to be specified (255 < 255,255,255) – quat Jun 21 '16 at 21:37

If you use enum instead of class, you save one character.

enum NoClass {
    F, G, H;    
    public static void main (String[] args) {

    }
}

But you have to introduce at least one enum instance (F, G, H in this example) which have to payoff themselves.

  • 1
    Seems you don't need any enum instances. I did enum M{;public static void main(String[]a){...} with no problems. – Danny Jan 27 '14 at 14:47
  • 3
    @Danny But then it doesn't save any characters. class M{ is exactly the same length as enum M{;. In that case, I'd go with the class because it is prettier (IMO) – Justin Feb 21 '14 at 23:32
  • at least for me enum{ worked without a ; after; its only the IDE moaning that there is a error butthe compiler accepts it – masterX244 Mar 5 '14 at 22:30
  • @masterX244 What compiler/version? Mine throws a tantrum and won't do it. – Geobits Apr 16 '14 at 14:04
  • worked on java 1.7 for me (appeared s, ned to investigate further cause with a update to .8 it stopped working) – masterX244 Apr 16 '14 at 17:41

When you have a method that should return a boolean or Boolean, i.e.:

// Return true if the (non-negative) input is dividable by 5
boolean c(int i){return i%5<1;}

You can change the boolean/Boolean return-type to Object to save 1 byte:

Object c(int i){return i%5<1;}

In addition, as you may have noticed, you can use a <1 check instead of ==0 to save a byte. Although that is more a general code-golf tip instead of Java-specific.
This is mostly used when the integer can't be negative, like checking for length:

a.length<1

instead of

a.length==0
  • 1
    Nice tip! You may want to add another example in the "if it can't be negative" section to illustrate it, since c(-21) returns true with the current one. – Geobits Jun 21 '16 at 20:54
  • Clarified. Also, don't you mean c(-20) instead of -21? -21 % 5 = 4 and -20 % 5 = 0. – Kevin Cruijssen Jun 21 '16 at 21:57
  • 1
    No, I meant -21. -21 % 5 != 4 in Java, which is my point. The divisible by five function would work correctly if modulus always returned non-negative, but it doesn't. See this example snippet. – Geobits Jun 21 '16 at 22:01
  • @Geobits Ah, thanks for the example. I almost never use negative numbers with %, so I forgot Java returns the remainder instead of the modulus, hence the difference.. – Kevin Cruijssen Jun 22 '16 at 8:54

Avoid StringBuilders

Appending stuff to a String takes up much fewer bytes.

// s is a StringBuilder
s.append("Hello, World!");

// S is a String
S+="Hello, World!";

If you have to reverse a string and print it right away, use a StringBuffer.

System.out.print(new StringBuilder("Hello, World!").reverse());
System.out.print(new StringBuffer("Hello, World!").reverse()); // Note that you can omit toString() when printing a non-String object

If you have to reverse a string and then do something else than printing it, use a foreach loop.

String b=new StringBuffer("Hello, World!").reverse().toString();
String B="";for(String c:"Hello, World!".split(""))B=c+B;
  • 3
    A foreach loop is shorter than StringBufferfor reversing strings. String b="";for(char c:"Hello, World!".toCharArray()){b=c+b;} – Poke Jun 21 '16 at 19:32
  • 1
    You should also remove the {} from that foreach loop if you're going to use that method. – Geobits Jun 21 '16 at 20:49
  • 1
    Save 2 bytes using String s:"".split("") instead of char c:"".toCharArray(). – charlie Aug 22 '16 at 19:02
  • If you have java.util.stream.Stream already imported, and if you need to chain another call to the result (like B.chartAt(42)) or if you just need to pass the result to a function (like f(B)), then using for(:) is equall to Stream.of("Hello, World!".split("")).reduce("",(a,b)->b+a). – charlie Aug 22 '16 at 19:18
  • Both lines in your example with the for-each can be golfed. First one can become: String b=new StringBuffer("Hello, World!").reverse()+""; (.toString replaced with +""), and your second line can become: String B="";for(String c:"Hello, World!".split(""))B=c+B; (char to String and .toCharArray() to .split("")). – Kevin Cruijssen Mar 24 '17 at 8:14

Use Java 10's var

If you define a single variable of a specific type, use var.

Examples

var i=0;                        // int
var l=0L;                       // long
var s="";                       // String
var a=new int[]{1,2,3};         // int[]
var i=java.math.BigInteger.ONE; // BigInteger
var m=new java.util.HashMap();  // HashMap
var i=3+"abc".length()          // int
var a="a b c".split(" ");       // String[]
for(var a:"a b c".split(" "))   // String

Not usable in any of the following examples

var cannot be used in many examples

var i=1,j=2;           // only one variable is accepted at a time
var a={1,2,3};         // arrays must be explicitly declared
var f=a->a+" ";        // can't know what type a is.
var f=String::replace; // method references aren't properly implied (weirdly, though)

In most cases, your program will be single-threaded, i.e it'll have only one thread running. You can exploit this fact by returning from the main method when you have to exit instantly.

static void main(String[]a){if(condition)return;}

Compare it to "properly" terminating the program:

static void main(String[]a){if(condition)System.exit(0);}

Or pointing to null:

static void main(String[]a){if(condition)throw null;}

Or dividing by 0:

static void main(String[]a){if(condition)int A=1/0;}

How to Draw in Java...

Here's the shortest possible GUI paint boiler-plate:

import java.awt.*;
static void main(String[]x){
    new Frame(){
        public void paint(Graphics g){
            // Draw your stuff here.
        }    
    }.show();
}

Golfed for 111 Bytes:

import java.awt.*;static void main(String[]x){new Frame(){public void paint(Graphics g){/*CodeHere*/}}.show();}

Sometimes, a single for-loop statement might be replaceable. Consider the following code:

int m(int x){int i=1;for(;x%++i==0;);return i;}

This is a simple for-loop which is a solution to this question.

Since we know that i will not be large enough to cause StackOverflow errors, we can replace the for-loop with recursion instead:

int m(int x,int i){return x%++i>0?i:m(x,i);}

We can simulate a loop by using a ternary operator in the return statement to cause recursion.

This reduction is rather specific, but I can imagine more situations where this would come in handy.

Using ... (varags) as parameter

In some cases it's shorter to use a Java varargs as parameter instead of loose ones.
For example:

// Example input/output: 5, 4, 3 -> 60000
int calculateVolumeInLiters(int width, int height, int depth){
  return width * height * depth * 1000;
}

Would be golfed by most to this:

int c(int w,int h,int d){return w*h*d*1000;} // 44 bytes

But can be golfed an additional byte to this:

int c(int...a){return a[0]*a[1]*a[2]*1000;}  // 43 bytes

Note that all three integers are only accessed once in the method itself. Since int is pretty short it is only beneficial if you use them each only once inside the method, and have three or more of them as parameter.

With longer parameters this is usually more useful though. For example, this was my original answer for this challenge (calculate occurances of input character in input string):

// Example input/output: tttggloyoi, t -> 3

int c(String a,char b){return a.replaceAll("[^"+b+"]","").length();} // 68 bytes

And I was recommended to golf it to this:

int c(String a,char b){return a.split(b+"").length-1;}               // 54 bytes

But I ended up golfing it to this using ...:

int c(String...a){return a[0].split(a[1]).length-1;}                 // 52 bytes

NOTE: If the question/challenge asks for a flexible input, the ... can be shortened to [] of course. If the question/challenge specifically asks for, let's say, three String inputs and disallows an String-array containing three values, you can use String... instead of String a,String b,String c.

  • 1
    Can't you use a String[] instead of using varargs? (saves 1 more byte) – Cows quack Mar 24 '17 at 8:13
  • @KritixiLithos Hmm.. good point. But that mainly depends on how flexible the input is for the challenge. If any input format is allowed, than that would indeed be shorter. I'll add this to this tips, thanks. – Kevin Cruijssen Mar 24 '17 at 8:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.