Wednesday, June 10, 2015

Executing Java code in comments

Well, today.. I spend my time at StackOverFlow and found a interesting question about executing Java code in comment. That freak me up, how a comment can be executed, right? By using non-ASCII characters, such example below this comment will be execute (line 3) :
public static void main(String... args) {
   // The comment below is no typo. 
   // \u000d System.out.println("Hello World!");
}
The reason for this is that the Java compiler parses the unicode character \u000d as a new line (line 4) and gets transformed into:
public static void main(String... args) {
   // The comment below is no typo. 
   // 
   System.out.println("Hello World!");
}

Whoah, programmer can insert malicious code or bring defect / bugs to the compiled software. So, I prefer and believe that an equivalent file containing only ASCII characters. Btw, some IDE block / disable this unicode character such Netbeans IDE.

Check the discussion HERE if you need more information.

No comments:

Post a Comment