r/programming • • May 10 '18

Java: Executing code in comments?!

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

84 comments sorted by

View all comments

163

u/kking254 May 10 '18

Actually executing code NOT in comments.

0

u/[deleted] May 10 '18

[deleted]

90

u/spacejack2114 May 10 '18
// \u000d App.initialize();
// Don't remove! ^ This comment is needed for some reason???

22

u/zergling_Lester May 10 '18

You can put it into what looks like a string literal even. As an example of sanitising unprintable characters, for example.

20

u/KnightMiner May 10 '18

Well, maybe we should say executing code no longer in comments, since it was in a comment at one time in its life

4

u/stronglikedan May 10 '18 edited May 10 '18

Since it was typed after the newline character characters, it was never in the comment. Comment slashes --> space (the comment) --> newline --> code

20

u/KnightMiner May 10 '18

Except technically it was not a newline character when it was typed, it was a collection of characters to be replaced with a newline.

1

u/stronglikedan May 10 '18

Fixed my previous comment. It was intended to represent a new line, same as pressing the enter key, so it was intended to end the comment. Therefore, the code was never part of the comment, as the author intended.

4

u/[deleted] May 11 '18

The code was after // and before a newline so yes it was IN comments.

7

u/kking254 May 11 '18

The code was after // then after a newline so no it wasn't IN comments.

FTFY

-1

u/[deleted] May 11 '18

I don't know if you know this but // \u000d is literally 9 characters and none of them has a decimal value of 10

3

u/coladict May 11 '18

I know you don't know this, but the article explains exactly why you're wrong! You would have known if you bothered to even skim it beyond the actual code in question.

-2

u/[deleted] May 11 '18

If you can read you'd understand I am saying the code is literally in the comment. I have other comments stating why it makes zero sense to decode the above and shows they're doing it wrong

3

u/recursive May 11 '18

Java compilers follow a specification, not what reddit posters say makes sense.

0

u/[deleted] May 12 '18

I'm not saying Java is doing something other than what it does. I'm saying not only does it make no sense it's doing what it is doing poorly and wrong

2

u/recursive May 12 '18

Correctness is defined by the spec.

0

u/[deleted] May 12 '18 edited May 12 '18

If the spec said + is subtraction and * is addition noone would say it's correct. Just like noone says only allowing one null in a unique column is a good idea (One of SQL Server biggest complaints)

→ More replies (0)