我刚刚发现我的编译器允许我编写下面的代码并且没有引发任何编译时错误。有谁可以赐教一下吗!
double y = arcToFindPointOn.getCenterXY().y - arcToFindPointOn.getRadius()*Math.sin(theta);;
上面代码行的奇怪之处在于最后的分号!
谢谢!
最佳答案
在其他语言中,例如 C# 和 C++,只有 ;
的指令意味着空指令,并且是允许的,不会生成任何错误,并且不会执行任何操作。
网上有很多这方面的文章,找到了这篇:Multiple semicolons are allowed by the C# Compiler for statement termination
This is a little weirdness inherited from C. In fact your example isn't a statement terminated by three semicolons, but three statements, two of which are empty. Occassionally it can be useful to use the empty statement in e.g. an if statement or a while loop, and then it would seem arbitrary to disallow it elsewhere. Besides, changing it now would be an unnecessary breaking change.
关于java - 为什么编译器没有出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7545375/