Reputation: 562
I would think that the following Java code would cause a RuntimeError (technically speaking) because it is referencing something non-existent (much like accessing the 8th element in an array of size 5).
int i;
System.out.println(i);
However, the IDE catches it, underlining it in red. Does that make it a syntax/compiler error? Or a runtime error that the IDE is just smart enough to catch?
Actually, is it a compiler error but not technically a syntax error? I always thought of them as synonymous, but maybe syntax errors are just a type of compiler error...
I know it's just semantics, but I'm teaching a class and feel silly not knowing what type of error it technically is.
Upvotes: 2
Views: 3195
Reputation: 719229
If int i;
is declaring a local variable, it is a compilation error to use it before it is assigned to; see @Eran's answer for the relevant section of the JLS.
Compilation error and compile-time errors are synonyms. (Compiler error is another synonym, though sometimes people use that to refer to bugs in the compiler.)
Does that make it a syntax/compiler error?
It is a compilation error. But it is not a syntax error.
This type of compilation error is typically called a semantic error.
A syntax error means that the code doesn't conform to the language's specified syntax (as defined by the grammar). In other words, the parser can't parse it.
A semantic error is any compilation error that isn't a syntax error. Depending on the programming language, semantic errors may include such things as:
Or a runtime error that the IDE is just smart enough to catch?
It is not a runtime error.
Actually, is it a compiler error but not technically a syntax error?
Correct.
I always thought of them as synonymous, but maybe syntax errors are just a type of compiler error...
They are not synonymous. Syntax errors are just one kind of compilation error.
(Unfortunately some Javascript implementations confusingly refer to all compilation errors as "Syntax Error". But that is not relevant if you are teaching Java. Java is not Javascript.)
Upvotes: 3
Reputation: 1
It's simple run time error. IDE would point that out, however if u run it you'll get some error like I needs to be initialise
Upvotes: -3
Reputation: 322
Have you tried compiling it without the IDE?
In Java it is detected as compile time error saying: error: variable i might not have been initialized
Upvotes: 0
Reputation: 393936
It's a compile-time error, as specified by the JLS:
14.4.2. Execution of Local Variable Declarations
A local variable declaration statement is an executable statement. Every time it is executed, the declarators are processed in order from left to right. If a declarator has an initialization expression, the expression is evaluated and its value is assigned to the variable.
If a declarator does not have an initialization expression, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs by the rules of §16.
Upvotes: 2