Hi folks,
An interesting observation. It's possible to execute Java code from inside what looks like comment line. You may call it a Java design flaw or a trick, below is the code piece
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
// I am a comment line | |
// \u000d System.out.println("Java On Java: Code Inside Comment Line"); | |
} | |
// Output | |
// Java On Java: Code Inside Comment Line |
So what's going on there? The trick is the unicode character in front of the comment line. It is parsed by Java compiler as a newline and hence the above code is read a below by the Compiler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
// I am a comment line | |
System.out.println("Java On Java: Code Inside Comment Line"); | |
} | |
// Output | |
// Java On Java: Code Inside Comment Line |
In Java Unicode decoding occurs earlier than any lexical translation. So the whole code gets converted into a unicode (not exactly, but it's easier to understand this way). This was done for Java code to be encoding free and truly platform independent.
Hence it's possible to write an entire code of Java in unicodes only. Still compiler will understand and run the program.
No comments:
Post a Comment