Executable Comments in Java

A comment is a statement that is not executed by the compiler or the interpreter, but before the lexical transformation of the program in the compiler, the contents of the program are encoded into ASCII to make the processing easier. Consider this program:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Main{
  
    public static void main(String[] args) {
        // The comment below is magic..
        // \u000d System.out.println("Geek Comment Executed!");
    }
}

chevron_right


This code will be executed and compiled successfully to produce the output.
Output

Geek Comment Executed!

This successfully produces this output because java compiler before lexical transformation parses the Unicode character \u000d as a new line and the program gets transformed into:



filter_none

edit
close

play_arrow

link
brightness_4
code

class Main{
      
    public static void main(String[] args) {
        // The comment below is magic
        //
        System.out.println("Geek Comment Executed!");
    }
}

chevron_right


The Unicode character Shifts the print statement to the next line which is then executed like a normal java program. So, the question arises why the translation of Unicode escapes happens before any other source code processing?

1. Java source code can be written in any encoding which allows a wide range of characters in string, literal and comments.
1. It makes processing by ASCII-based tools easier.
2. This guarantees java Platform dependence which is the independence from supported character sets.
3. This helps in documenting code in non-Latin languages.

Being able to write any Unicode character anywhere in the file is a neat feature, The fact that it can interfere with the semantics in such subtle ways is just a side-effect.

Let’s consider this java program:

filter_none

edit
close

play_arrow

link
brightness_4
code

\u0070\u0075\u0062\u006c\u0069\u0063\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

chevron_right


Output:

Hello World

This unicode program is converted into its representative values to produce “Hello World”
Reference
https://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed?newsletter=1&nlcode=222379%7C1266



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.