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.
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)
ifmain
is declared aspublic static void main(String[]a)
. – Dennis – 2015-09-02T18:15:42.840Out 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
Related: http://codegolf.stackexchange.com/questions/13152/shortest-code-to-produce-infinite-output?rq=1
– Kzqai – 2015-09-03T00:24:19.197@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