r/coding • • May 10 '18

Java: Executing code in comments?!

http://programming.guide/java/executing-code-in-comments.html
112 Upvotes

8 comments sorted by

View all comments

33

u/rigatron1 May 10 '18 edited May 10 '18

+/u/CompileBot Java

\u0020\u0020\u0020\u0020
\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079
\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020
\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063
\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028
\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020
\u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b
\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074
\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020
\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b
\u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d

28

u/CompileBot May 10 '18

Output:

Hello world

^source | ^info | ^git | ^report

20

u/cogman10 May 10 '18

I'm not sure if I should be happy or sad.

-13

u/wung May 10 '18

In the example above? There is no reason to be happy or sad. It literally is a byte mess. Having any "reasonable" assumptions based on the non-readable form about it is absurd. Is a hexdump of a valid program absurd? No, it is not. Of course it will compile, it is just another encoding.

14

u/cogman10 May 10 '18

It is not a hex dump nor is it another encoding. I would be far more forgiving if it were.

This is the compiler swapping in unicode values when it sees \uxxxx . These are not unicode values. These sorts of "helps" are awful for a language. Especially since you can have UTF-8 source code with java. Which means, in a UTF-8 file you can throw in \uxxxx escapes.

This is nasty for the same reason trigraphs in C++ are a bad idea.