Tuesday, September 28, 2004

Java Quiz - Win a GMail Invitation

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:

Anonymous said...

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

Anonymous said...

whats wrong with that code that will prevent it from compiling???

Anonymous said...

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.

Anonymous said...

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

Anonymous said...

Let us know who got the Gmail invite!! Also the exact reason for the successful compilation.

Anonymous said...

This program wont compile because of extra closing brace