Java Puzzle: Commented Code Compiles

Last modified on September 23rd, 2014 by Joe.

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 Java 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 that this Java code should compile without any errors. 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?

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

Comments on "Java Puzzle: Commented Code Compiles"

  1. Pushkar says:

    Wonderful Example.
    Thanks a lot again for adding one more concept in my Knowledge.

    Regards,
    Pushkar

  2. Kishore says:

    Thats a nice puzzle which is solved.. Thanks

  3. Anonymous says:

    GRT example

  4. Bhrugu Joshi says:

    Great !

  5. Neeraj Kumar says:

    Nice Puzzle. Thanks.

  6. Nagendra says:

    Good one.. Thanks …

  7. dipesh says:

    really useful

  8. prabhu says:

    Great artical

  9. srinidhi nakshatri says:

    Thanks Joe.

  10. ashok says:

    nice one

  11. Vinodh says:

    Nice puzzle with example

  12. Ashhutosh kumar says:

    No Thats not seems true as Character myChar = new Character(‘abcd’); is not valid. initialization so new Character(‘\u000d’); is also

    its not a Character like ‘a’

  13. Mangal says:

    “Do not rely on comments to save something for the future. Use the version control system for that”

    This was the take away point for me. Thanks :)

  14. Hemanth says:

    I am Looking the pzl for first time ..

    so as u mentioned it was surprising to me

    thanx

  15. Anonymous says:

    Hi,thanks for sharing such a useful info “Do not rely on comments to save something for the future. Use the version control system for that” :)

  16. Monark says:

    Chicky !!!

  17. Amit Shrama says:

    Very Nice Example…. Carry On Sir

  18. Pavithra says:

    Awesome!!!!

  19. Praveen says:

    Good One Joe ! Thank you..

  20. Pratik says:

    Hi Joe,
    I guess it happens before compilation and the step is called “Unicode Preprocessor”. Correct me if I m wrong…

    Thanks

  21. Shankar says:

    Its awesome!!!! Do not rely on comments to something for future…..!!!!!!!

  22. Joju says:

    Nice puzzle. Thanks for sharing :)

  23. Sorabh Trivedi says:

    You try : /*Character myChar = new Character(‘\u000d’);*/
    ??

  24. Siva Narayana Reddy M says:

    I have one doubt.
    Why is the compiler looking into the commented code and trying to replace the unicode ‘\u00d’ with its actual new line character value?
    I thought that compiler will not consider the commented code and will ignore it.
    I am still not sure how this is possible..

  25. Arup says:

    Just follow what is being said in this example.Like above,your example will look like
    /*Character myChar = new Character(‘
    );*/

    this is absolutely fine for no compilation error.
    Thanks Joe for this tricky example

  26. Poonam Gaigole says:

    Awesome tutorials… Very intersting. I love java gallery and java puzzles

  27. Mayank says:

    nice thank you

  28. kamal nayan says:

    Good one… Thanks :)

Comments are closed for "Java Puzzle: Commented Code Compiles".