Wednesday, December 4, 2013

Java Puzzle: Commented Code Compiles

This one is a nice Java puzzle. If you know it already then its very cheap else it will be surprising. These puzzles come out of SCJP preparation mostly. That is where these kind of intriguing code gets cooked up. Long time back, I shared a Java puzzle on unreachable statement and it is similar to this.
Following is the puzzle code,
public class Puzzle {
  public static void main(String... args) {
    System.out.println("Hi Guys!");
    Character myChar = new Character('\u000d');
   }
}
javac Puzzle.java and we get following errors:
Puzzle.java:4: error: illegal line end in character literal
                Character myChar = new Character(‘\u000d’);
                                                 ^
1 error

One thing is clear that, there is something wrong with the ‘myChar’ line. So, commented that line to make this work for now (so that is what we do in development right) and the code now looks as below,
public class Puzzle {
  public static void main(String... args) {
    System.out.println("Hi Guys!");
//    Character myChar = new Character('\u000d');
   }
}
javac Puzzle.java and we get following errors:
Puzzle.java:4: error: unclosed character literal
//              Character myChar = new Character(‘\u000d’);
                                                        ^
1 error


The expectation is the code should compile now. Since there is only one System.out.println statement and that is straight forward. But what we get is an error and that too out of the commented code.
Let there be anything inside the commented code, how can the Java compiler complain about it. So by this time you might have found the issue.
puzzle

Solution to the Puzzle

Character myChar = new Character(‘\u000d’);
In the above line, we are trying to instantiate a unicode character. It is ok, syntactically we can do something like this. But the problem is with the unicode character we trying to instantiate. \u000d represents a newline character in unicode.
Java compiler, just before the actual compilation strips out all the unicode characters and coverts it to character form. This parsing is done for the complete source code which includes the comments also. After this conversion happens then the Java compilation process continues.
In our code, the when Java compiler encounters \u000d, it considers this as a newline and changes the code as below,
public class Puzzle {
  public static void main(String... args) {
    System.out.println("Hi Guys!");
//    Character myChar = new Character('
    ');
   }
}
In the above Java code, look at line 5. It starts with a single quote and there is no following ending quote. Single quote is for declaring a character literal and so we get the error ‘unclosed character literal’ from the Java compiler.
Not because of this, its always better to remove the commented code from the source. Do not rely on comments to save something for the future. Use the version control system for that

No comments:

Post a Comment