According to my scientific Java experimentation, int x = 0; is equivalent to int x = 0;; which is equivalent to int x = 0;;;;;;;;;;;;;;;
- Why does Java allow for this? Does it have any practical application?
- Are each of these just empty statements? Do they actually take up any extra processing time during runtime? (I would assume they’re just optimized out?)
- Do other languages do this? I’m guessing it’s something inherited from C, like a lot of things in Java. Is this true?
As Jake King writes, you can produce empty statements to do nothing in a loop:
but to make it obvious, you would write
or even better:
or even better, as Michael Kjörling pointed out in the comment,
More often, you see it in for-statements for endless loops:
or only one empty statement
Another thing you can do, is, to write a program, once with one, and once with 2 semicolons:
Compile it, and run list the size of byte code (identic) and do an md5sum on the bytecode (identic).
So in cases, where the semantics aren’t changed, it is clearly optimized away, at least for the 1.6-Oracle compiler I can say so.