What is the shortest infinite loop statement in Java?

8

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.

corsiKa

Posted 2015-09-02T18:04:15.340

Reputation: 397

Or something like main();? – jimmy23013 – 2015-09-02T18:08:32.877

@jimmy23013 No, but you can do main(null). – Ypnypn – 2015-09-02T18:14:06.373

@Ypnypn Or main(a) if main is declared as public static void main(String[]a). – Dennis – 2015-09-02T18:15:42.840

Out of interest do Java compilers or JITs typically optimize tail-call recursion, or are these recursions limited by stack size? – Steve Jessop – 2015-09-02T23:39:55.180

@SteveJessop I imagine a lot of loops are unrolled, tail-recursion optimized, or many other tricks. I believe sometimes they will unroll the loop for part of the loop, but not for others - like for(i = 0; i < 100; i++) { /* stmts */ } will maybe unroll ten statements, and make the for loop execute ten times. Irrelevant from the source code perspective, but still super cool! – corsiKa – 2015-09-03T02:03:34.593

@jimmy23013 You may have provided me a way to do my the shortcut that prompted this question using recursion actually. It might not answer this particular question, but it might have made the underlying code I was making a bit shorter. It's not like I care how big my stack gets as long as it doesn't overflow! – corsiKa – 2015-09-03T02:05:45.410

A little off topic, I'm surprised we see more Java than Groovy here. If you like Java you should give Groovy a try for code golfing, It's Java with a lot of shortcuts : implicit main(), some of python syntax, a lot of overriden operators, truthy / falsey values, etc. ; In Groovy, while (1) would work. – Aaron – 2015-09-03T09:34:22.530

Java very explicitly does not perform tail-call elimination. It's not actually an optimization because it modifies the semantics of programs. It obviously makes the difference between programs which crash on a stack overflow versus loop forever. It also impacts any code which inspects and manipulates stack traces. – JohnE – 2015-09-04T23:05:34.627

@JohnE holy shit... I have only one word: CogParticle – corsiKa – 2015-09-06T05:13:31.947

@corsiKa: I'm not proud of all my past code. As Rufus says in Bill and Ted's Excellent Adventure, "they do get better..." – JohnE – 2015-09-06T14:46:02.640

Answers

14

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

masterX244

Posted 2015-09-02T18:04:15.340

Reputation: 3 942

6If you have multiple statements, try to put all but one in the for loop itself. e.g., for(;;foo(),bar(),baz())quux(); – Ypnypn – 2015-09-02T18:14:59.447

2you can exchange the empty braces for a semicolon for(;;); – ratchet freak – 2015-09-02T22:35:54.307

@ratchetfreak Only if you don't intend to break the loop, but the OP that they do want the ability to break. – Alex A. – 2015-09-03T00:14:42.440

@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 – 2015-09-03T02:07:08.000

@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 – 2018-10-06T23:19:56.667

1

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.

user15259

Posted 2015-09-02T18:04:15.340

Reputation:

1problem with your solution is, it will give you quite quickly StackOverflowError so it hard to call it infinite loop – user902383 – 2016-02-12T11:10:57.820