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?
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 are closed for "Java Puzzle: Commented Code Compiles".
Wonderful Example.
Thanks a lot again for adding one more concept in my Knowledge.
Regards,
Pushkar
Thats a nice puzzle which is solved.. Thanks
GRT example
Great !
Nice Puzzle. Thanks.
Good one.. Thanks …
really useful
Great artical
Thanks Joe.
nice one
Nice puzzle with example
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’
“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 :)
I am Looking the pzl for first time ..
so as u mentioned it was surprising to me
thanx
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” :)
Chicky !!!
Very Nice Example…. Carry On Sir
Awesome!!!!
Good One Joe ! Thank you..
Hi Joe,
I guess it happens before compilation and the step is called “Unicode Preprocessor”. Correct me if I m wrong…
Thanks
Its awesome!!!! Do not rely on comments to something for future…..!!!!!!!
Nice puzzle. Thanks for sharing :)
You try : /*Character myChar = new Character(‘\u000d’);*/
??
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..
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
Awesome tutorials… Very intersting. I love java gallery and java puzzles
nice thank you
Good one… Thanks :)