Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I know I know, not another tips question. But I'm rather surprised that this question hasn't already been posted. At any rate, are there any useful shortcuts that can be used especially 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.

share|improve this question
1  
package can be skipped. – st0le Jul 19 '12 at 7:08

4 Answers

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
}
share|improve this answer
+1 Excellent resource! I'll be sure to play around with it. – Mike Dtrick 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? – Mike Dtrick Sep 16 '12 at 13:45

With a static import:

import static java.lang.System.out;

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");    
}
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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