Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue of syntax or semantics?

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?

like image 472
Adeeb Avatar asked Jan 10 '12 13:01

Adeeb


1 Answers

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 ;) ).

like image 175
Ernest Friedman-Hill Avatar answered Nov 03 '22 01:11

Ernest Friedman-Hill