This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiler checks first syntax and then other errors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiler checks first syntax and then other errors?" Watch "Compiler checks first syntax and then other errors?" New topic
Author

Compiler checks first syntax and then other errors?

Al Morrissey
Greenhorn

Joined: Jul 19, 2005
Posts: 3
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

Joined: May 31, 2005
Posts: 519

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
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?


Devender Thareja
SCEA, SCBCD, SCJP
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

My bad, i did finish readinf the code properly.
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
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.


SCJP 1.4, SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiler checks first syntax and then other errors?
 
Similar Threads
Shadowing variable doubt?
Code segment question #3(5)
switch
garbage collection
multi-dimension array question - dan's exam