The method below cannot be executed in Java because the variable i
may remain uninitialized by the time of its use. Is this an issue of syntax or semantics?
public int odd( boolean b ){
int i;
if( b ){ i = 3;}
return i;
}
I would've thought it would be semantics, but according to my instructor it is a syntax error. Is that correct, and why?
It's an error detected by the Java compiler, but it's not a syntax error; it's perfectly valid according to the Java grammar. It's detected in later stages of analysis, making it a semantic error.
That said, it sounds like your instructor wants to define any compiler error as a syntax error, and probably wants "semantic error" to mean something that goes wrong at runtime. Since the instructor grades the homework, you're forced to accept his definitions (even if they are completely wrong, as in this case ;) ).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With