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!
The
;makes Java think that the body of theifconditional is complete, even if there is no other code preceding it. In effect, the code in theifstatement 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 theifblock.edit: here’s another stack overflow question about the
{ }blocks: What do curly braces in Java mean by themselves?