# What is the shortest infinite loop statement in Java?

I'm working on a golf for Java and I need an infinite loop. Obviously, I don't want to spend any more bytes than I have to, especially in such an expressive language.

Assuming I have some code I want to run, obviously the baseline is set at while(1>0)/*stmt*/ or 10 additional characters for a single line and while(1>0){/*stmt1*//*stmt2*/} or 12 additional characters for multiple lines. I say additional because the code I want to loop forever (well, forever-ish... I may or may not want to break out of or return from the loop) will have a certain length, and then I must tack on additional characters to make it actually loop.

At first I thought this was the best I'd get, but I figured I'd throw it out to the experts to see if they can find a better one.

• Or something like main();? – jimmy23013 Sep 2 '15 at 18:08
• @jimmy23013 No, but you can do main(null). – Ypnypn Sep 2 '15 at 18:14
• @Ypnypn Or main(a) if main is declared as public static void main(String[]a). – Dennis Sep 2 '15 at 18:15
• Out of interest do Java compilers or JITs typically optimize tail-call recursion, or are these recursions limited by stack size? – Steve Jessop Sep 2 '15 at 23:39
• – Kzqai Sep 3 '15 at 0:24

for(;;){}


It works cause no condition evaluates to always true. Insert the code between the braces.

If you only got a few statements you can place them inside the head (the (;;) part. That the loop still runs is caused by when the condition statement is not a boolean it counts as always true. Thanx to @Ypnypn for the reminder

• If you have multiple statements, try to put all but one in the for loop itself. e.g., for(;;foo(),bar(),baz())quux(); – Ypnypn Sep 2 '15 at 18:14
• you can exchange the empty braces for a semicolon for(;;); – ratchet freak Sep 2 '15 at 22:35
• @ratchetfreak Only if you don't intend to break the loop, but the OP that they do want the ability to break. – Alex A. Sep 3 '15 at 0:14
• @ratchetfreak Yes I'm looking for both single and multi-line, although it's debatable whether Ypnypn's suggestion might actually be smaller. Either way, I should edit my post to make my objective more clear. – corsiKa Sep 3 '15 at 2:07
• @Ypnypn I don't think that's legal syntax. There's no comma operator in Java (though it can be used in variable declarations). – Tom Hawtin - tackline Oct 6 '18 at 23:19

# Java - 60 bytes (complete code)

I'm probably not interpreting the question correctly, but this code when compiled and run results in an infinite-ish loop, in that technically it should run forever, but usually it will exhaust the stack memory.

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


On Coding Ground it ends quite quickly, on my Windows 7 laptop with advanced virtual memory management, it runs a long time, just making the system slower and slower.

• problem with your solution is, it will give you quite quickly StackOverflowError so it hard to call it infinite loop – user902383 Feb 12 '16 at 11:10

# 50 bytes

interface M{static void main(String[]a){for(;;);}}