How to execute Java code within Java comments

31 / May / 2016 by Sandeep Gupta 1 comments

We all know Java comments are not visible to compiler and are not executable. But here we will see how a comment can be executed. To execute any comment we need to append a unicode as prefix to that comment and thats it. Now when you will run the code then this comment line will be executed.

public class CommentTest {
	public static void main(String[] args)
		// Following line will be executed and will produce output.
		// \u000d System.out.println("This is the output generated from commented line");
This is the output generated from commented line

Why this is happening? Following is the explanation taken from  Java Language Specification – Section 3.3

The Java programming language specifies a standard way of transforming a program written in Unicode into ASCII that changes a program into a form that can be processed by ASCII-based tools. The transformation involves converting any Unicode escapes in the source text of the program to ASCII by adding an extra u – for example, \uxxxxbecomes \uuxxxx – while simultaneously converting non-ASCII characters in the source text to Unicode escapes containing a single u each.


comments (1 “How to execute Java code within Java comments”)

Leave a comment -