为什么没有编译时错误?奇怪的-Java

发布于 2024-12-05 23:40:27 字数 497 浏览 0 评论 0原文

我有一个 if 语句检查某些值,并遇到一个奇怪的错误(不确定!)。我的代码在语法上不正确,因此产生了错误的结果,但是 eclipse 在编译时没有引发任何错误。为什么我的下面的代码有效?

if((this.trackPointList.get(point).getTurnOutId().equals(seg.getSegRef().getTurnOut())) && seg.getSegRef().getKind().equals("arc")); // <---- See here I have semicolon
    {
       ... code to run ...
    }

上面的代码仅检查第一个条件并忽略 seg.getSegRef().getKind().equals("arc") 但我想这应该在编译时引发问题,我对吗?当我通过逐行浏览进行调试并找到这个分号时,我的逻辑就起作用了。如果有人能解释它是否是有效的语法,我将不胜感激。

请赐教!

I had an if statement checking some value, And encountered a weird bug(Not sure!). My code was incorrect syntactically and in result it produced a wrong result, however eclipse didn't raised any error while compiling. Why My below code worked?

if((this.trackPointList.get(point).getTurnOutId().equals(seg.getSegRef().getTurnOut())) && seg.getSegRef().getKind().equals("arc")); // <---- See here I have semicolon
    {
       ... code to run ...
    }

Above code check only first condition and ignores seg.getSegRef().getKind().equals("arc") but I guess this should raised an issue at compile time, Am I right? My logic worked once I debugged it by skimming line by line and found this semicolon. I will appreciate if someone could explain, if it is a valid syntax.

Enlighten Me, Please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪也成诗 2024-12-12 23:40:27

; 使 Java 认为 if 条件的主体是完整的,即使其前面没有其他代码。实际上,执行了 if 语句中的代码,但不存在主体,因为存在 ;

{ ...code to run...} 只是一个执行的代码块,该块内声明的任何内容在该块外都不可见。它总是在这里运行,因为它不是 if 块的一部分。

编辑:这是关于 { } 块的另一个堆栈溢出问题:Java 中的花括号本身意味着什么?

The ; makes Java think that the body of the if conditional is complete, even if there is no other code preceding it. In effect, the code in the if statement is executed, but no body exists because the ; is there.

The { ...code to run...} is just a code block that executes, and anything declared inside that block is not visible outside the block. It will always run here because it's not part of the if block.

edit: here's another stack overflow question about the { } blocks: What do curly braces in Java mean by themselves?

最后的乘客 2024-12-12 23:40:27

该代码在语法上是正确的。您可以编写不带大括号的 if,例如:

if(condition) statement;

具有空语句也是有效的。例如,这段代码是有效的:

int a = 0;;
;;;

所以空的 if 也是有效的,尽管它没有多大意义:)

if(condition);

The code is syntactically correct. You can write ifs without braces, like:

if(condition) statement;

Having empty statements is also valid. For instance this code is valid:

int a = 0;;
;;;

So an empty if is valid as well, although it doesn't make much sense :)

if(condition);
风吹雪碎 2024-12-12 23:40:27

后跟分号的 if 语句称为“空 if 语句”。
它很少有任何用处,但它在语法上是合法的。

您可以写这样的内容

if ( doSomethingThatReturnsABoolean() )
   ;  // Empty statement
else
   doSomeOtherThing()

,但最好写成

if ( !doSomethingThatReturnsABoolean() )
   doSomeOtherThing()

根据您的观察,仅检查第一个条件:
如果第一个条件返回 false,则不会检查第二个条件,因为

(false && secondCondition)

始终等于 false,因此第二个条件的值无关紧要。

An if statement followed by a semicolon is called an "empty if statement".
It rarely is of any use, but it's syntactically legal.

You can write something like this

if ( doSomethingThatReturnsABoolean() )
   ;  // Empty statement
else
   doSomeOtherThing()

but it would be better to write

if ( !doSomethingThatReturnsABoolean() )
   doSomeOtherThing()

Regarding your observation that only the first condition gets checked:
If the first condition returns false the second condition will not get checked, because

(false && secondCondition)

always equals to false, so the value of secondCondition is irrelevant.

別甾虛僞 2024-12-12 23:40:27

if(..) 语句后面的 ; 表示当 if(..) 计算结果为 true 时有条件执行的空语句。 { .. } 表示始终在其自己的范围内执行的代码块。

如果由于短路评估导致第一个条件为假,则可以忽略第二个条件。

The ; after the if(..) statement represents an empty statement that is executed conditionally when the if(..) evaluates to true. The { .. } represents a code block that always executes with it's own scope.

The second condition may be ignored if the first condition is false due to short-circuit evaluation.

浪推晚风 2024-12-12 23:40:27

if(condition); 相当于 if(condition){}

for 循环 & 相同。 while 循环

for(;condition;); 等价于 for(;condition;){}

while(condition); 相当于 while(condition){}

if(condition); is equivalent to if(condition){}

same thing with for loop & while loop:

for(;condition;); is equivalent to for(;condition;){}

while(condition); is equivalent to while(condition){}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文