Why does the following program compile:
public class Kumar {
public static void main(String args[]) {
// Hoping this code will not compile ?#$%&**\u000d{
for (int i=0; i<10; i++) {
System.out.println("Hello");
}
} //Additional close braces
}
}
The first one to answer gets a free GMail invitation. Answer to be posted tomorrow if no one get posts the correct answer.
6 comments:
My guess is that the \u000d in the comment is parsed as a newline, thus ending the comment, causing the { to function as "start of block".
But I already have a GMail account, so...
/Robert
whats wrong with that code that will prevent it from compiling???
To counter the previous commenter; it's because the \u000d does _not_ act as an end-of-line marker.
You have an extra closing brace; delete the one marked with the "Additional close braces", or put a real new-line marker in after the \u000d.
Piece of cake:
Section 3.2 of the Java Language Specification (second edition) specifically requires that the compiler first process Unicode escapes (such as \u000d), then look for line terminators to break the input into lines.
Section 3.4 of J2LS defines a LineTerminator as CR, LF, or CRLF. \u000d is a CR, and thus is a LineTerminator.
Section 3.7 of J2LS specifies that a // comment (EndOfLineComment) is terminated by a LineTerminator character.
Thus, the // comment ends with the \u000d. The opening brace that follows is considered to be on a new line, so the additional closing brace is needed in order to match it.
-- Doug
Let us know who got the Gmail invite!! Also the exact reason for the successful compilation.
This program wont compile because of extra closing brace
Post a Comment