Java – why compile code with consecutive semicolons?

According to my scientific Java experiment, int x = 0; Equivalent to int x = 0;; This is equivalent to int x = 0;;;;;;;;;;;;;;

>Why does Java allow this? Is there any practical application? Are these all empty? Do they actually take up any extra processing time at runtime? (I assume they just optimized?) > Do other languages do this? I guess it's something inherited from C, just like many things in Java It's true?

Solution

As Jake King wrote, you can generate an empty statement and do nothing in the loop:

while (condition);

But be clear, you can write

while (condition)
    ;

Even better:

while (condition)
/** intentionally empty */
    ;

Even better, as Michael kJ ö rling pointed out in his comments,

while (condition)
{
    /** intentionally empty */
}

More often, you will see it loop indefinitely in for statements:

for (;;)

Or there is only one empty statement

for (start;;) 
for (;cond;) 
for (;;end)

Another thing you can do is write a program, one at a time, with two semicolons at a time:

public class Empty
{
    public static void main (String args[])
    {
        System.out.println ("Just semicolons");;
    }
}

Compile it, run the size of the list bytecode (ID), and make an md5sum on the bytecode (ID)

Therefore, it is explicitly optimized with the same semantics, at least for the 1.6-oracle compiler

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>