在java中只允许在代码中保留分号.现场背后会发生什么?编译器和JVM如何处理它们?

Abh*_*ngh 1 java

public static void main(String[] args) {
    File dir = new File("dir");
    dir.mkdir();

    File file = new File(dir,"file.txt");;;;;
    ;
    ;
    ;
    try {
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        ;
        ;
        ;
        ;
    }



}
Run Code Online (Sandbox Code Playgroud)

编译器显示额外的分号没有错误.并且代码运行就好像没有发生任何错误.我想知道幕后发生了什么?包括这样的分号是否会消耗更多的堆栈内存,因此需要更多的处理器周期才能运行?

aio*_*obe 9

我想知道幕后发生了什么?

额外;出现在AST中作为跳过语句.

它们通常用于代替空体,例如while循环:

while (expression)
    ;
Run Code Online (Sandbox Code Playgroud)

包括这样的分号是否会消耗更多的堆栈内存,因此需要更多的处理器周期才能运行?

不,它不会出现在字节代码中.(这就是为什么它们在调试时通常不能用作break语句.)


一些琐事:

实际上,你可能之外有跳过语句:

class MyClass {
    // ...
}

;;;;;
Run Code Online (Sandbox Code Playgroud)

这些都被忽略了,并且只是为了不让人们厌倦来自C++并习惯于;在课后放置:

class MyClass {
    // ...
};
Run Code Online (Sandbox Code Playgroud)

来源:我是Oracle的编译器开发人员