Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiler checks first syntax and then other errors?

 
Al Morrissey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class JMM124 {
public static void main(String args[]) {
int k;
for (int i=0, j=0; i<2; i++,j++) {System.out.print(i);} // 1
for (int i=0, k=0; i<2; i++,k++) {System.out.print(i);} // 2
for (int i=0, int j=0; i<2; i++,j++) {System.out.print(i);} // 3
}
}

In which line it compilation error
A) line 1
B) line 2
C) line 3
D) none

I said line 2, but I was wrong: the correct answer is line 3- compiler .
My question is what is the order the compiler works? Because if we comment the line 3, the compiler complain about line 2.
It seems that first the compiler checks all file for syntax error then for other errors. This is how I understood this situation, but I do not know all the rulles.

Thanks
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will complain at line 3 because after this line the method ends, and it sees that the variable k has not been explicitly initialized.

If we comment this line out, it will complain at line 2, because of the same reason as above, it sees that the method is ending without a value beeing asigned to the int variable k.

Cheers, Marzo.
 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marzo Kaahn:
The compiler will complain at line 3 because after this line the method ends, and it sees that the variable k has not been explicitly initialized.

If we comment this line out, it will complain at line 2, because of the same reason as above, it sees that the method is ending without a value beeing asigned to the int variable k.

Cheers, Marzo.


The line 2 and line 3 both have error.
It's not because k is initialized. It has been initialized in line 2.
The error is that compiler is not expecting key word "int" in int j=0 in initlization part in line 3. If you remove int, it works fine.
The question is why it complains for line 3 first, if you correct it then it reports error on line2.
Is it because compiler works backward?
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad, i did finish readinf the code properly.
 
Sergei Iakhnin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Al Morrissey:
It seems that first the compiler checks all file for syntax error then for other errors. This is how I understood this situation, but I do not know all the rulles.


I think you are right on this one. It makes sense too because if there are syntax errors the program cannot be parsed correctly (ie. code breaks the language Context Free Grammar) and things like doubly declared variables (which is what the problem on line 2 is) can only come afterwards since the bounds of all code blocks in the file have to be determined to deduce variable scope.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic