有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么编译器没有引发错误?

我刚刚发现我的编译器允许我写下面的代码,并且没有引发任何编译时错误。谁能开导我一下吗

double y = arcToFindPointOn.getCenterXY().y - arcToFindPointOn.getRadius()*Math.sin(theta);;

上面代码行的奇怪之处是结尾处的分号

谢谢


共 (3) 个答案

  1. # 1 楼答案

    Java还允许empty statement

    ;
    

    实际上什么都没做。在你的例子中,你只在最后有一个

  2. # 2 楼答案

    其他语言中,如C++和C++一样,只有^ {CD1>}的指令意味着空指令,并且允许,不产生错误,简单地不做任何事情。p>

    网上有很多关于这个的文章,找到了这个: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.

  3. # 3 楼答案

    “;”是一个定界符,因此编译器可以安全地忽略空白语句。然而,一些IDE可以帮助您优化代码,并且不会让您逃脱这种说法的惩罚