gpt4 book ai didi

java - 在 java 中只允许在代码中保留分号。幕后发生了什么?编译器和 JVM 如何对待它们?

转载 作者:行者123 更新时间:2023-11-29 09:53:07 24 4
gpt4 key购买 nike

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
;
;
;
;
}



}

编译器没有显示额外分号的错误。代码运行起来就好像没有发生任何错误一样。我想知道幕后发生了什么?包含这样的分号是否会消耗更多的堆栈内存,从而需要更多的处理器周期来运行?

最佳答案

I want to know what happens behind scene ?

额外的 ; 在 AST 中作为 skip 语句出现。

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

while (expression)
;

Does including such semi-colon consume more of stack memory , and thus require more processor cycles to run ?

不,它没有出现在字节码中。 (这就是为什么它们在调试时通常是 can't be used as break statements。)


一些琐事:

你实际上可能在类的之外有 skip 语句:

class MyClass {
// ...
}

;;;;;

这些被忽略,并且允许只是为了不惹恼来自 C++ 的人并且习惯于将 ; 放在类之后:

class MyClass {
// ...
};

资料来源:我是 Oracle 的一名编译器开发人员

关于java - 在 java 中只允许在代码中保留分号。幕后发生了什么?编译器和 JVM 如何对待它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29192026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com