Teacher, are you sure Java annotations will not be executed?

Previously, I shared an article on my blog about comments in Java and wrote a word with oath: "Comments will not be executed!"As a result, a message from a partner said, "Are you sure, teacher?"

I have always had the advantage of being able to hear other people's voices, whether you are complimentary or critical, and always accept them with confidence.Because I believe most of my little friends are for good purposes.

What's more, I never thought technically about being a bully, just like sharing.I keep the errors in many articles as they are, because if they are corrected, the people who point out the errors in the message will feel inappropriate to subsequent readers.

Those diss my little buddies, rest assured, I won't mind.

Nevertheless, it is really unbearable to comment on this matter!Notes will never be executed, I think this little buddy must be mocking me.So I asked him what he was doing and he dumped the following code for me:

public class Test {
    public static void main(String[] args) {
        String name = "Silent King Two";
        //\000dname="Silence King Three";

I copied it to IDEA and ran, and the result was unexpected:

Silent King Three

It was King Three, not King Two.Seeing this result, I was completely confused.

At that moment, I felt that Java has been learning for more than a decade.At the University meeting, the teacher said that comments would not be executed; even in Programming Thoughts, notes would not be executed.So now who can tell me exactly why?

Isn't the world of programs simple?Either 0 or 1?Now that things have come to this point, we can only take the time to study them.

Just from the code point of view, the problem should be on that special string of characters - \u000d. If it's not funny, there is no other reason to change the value of name from "Silent King 2" to "Silent King 3". No other reason, with years of work experience, I'm quite comfortable finding the source of the problem.

u 000d looks strange, but I know it's a Unicode character.After asking the search engine, I know it represents a line break - a feeling of insight.I know that the Java compiler not only compiles code, but also parses Unicode characters.

I take a glance at the compiled byte code above, which looks like this:

// class version 58.65535 (-65478)
// access flags 0x21
public class com/cmower/dzone/secret/Test {

  // compiled from: Test.java

  // access flags 0x1
  public <init>()V
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    LOCALVARIABLE this Lcom/cmower/dzone/secret/Test; L0 L1 0
    MAXSTACK = 1

  // access flags 0x9
  public static main([Ljava/lang/String;)V
    LDC "\u6c89\u9ed8\u738b\u4e8c"
    ASTORE 1
    LDC "\u6c89\u9ed8\u738b\u4e09"
    ASTORE 1
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 1
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
    LOCALVARIABLE args [Ljava/lang/String; L0 L4 0
    LOCALVARIABLE name Ljava/lang/String; L1 L4 1
    MAXSTACK = 2

Well, it means you can't read it.But that's okay, just decompile it, so I see this code:

public class Test {
    public Test() {

    public static void main(String[] args) {
        String name = "Silent King Two";
        name = "Silent King Three";

Well, the two backslashes //are really missing, which confirms one thing - the comment really won't execute.It's just u 000d pushing name= "Silent King III"; to the next line of the //comment, just like this code:

public class Test {
    public static void main(String[] args) {
        String name = "Silent King Two";
        name="Silent King Three";

So isn't that a Java bug?Say nothing.

Because by allowing Java source code to contain Unicode characters, you can ensure that code written in any region of the world executes elsewhere.

To be honest, I found this from the Internet, as if I knew something, but I didn't.Let's take another look at the code:

double π = Math.PI;

If programmer Xiao Wang doesn't know how to type out the character PI when he creates the variable of cycle rate, he can choose to useu03C0 instead - the compiler knows thatu03C0 is the variable PI (the compiler parses the Unicode character before compiling other code).

Only u 000d is an exception.

Of course, unless special circumstances arise, do not include Unicode characters in the source code to avoid changing the meaning of the source code.

This article is meaningless, and I don't want to explore something too deep, just to raise the awareness of my little buddies that comments may be executed by the compiler.As if Lu Xun didn't know there were four ways to write the word fennel in fennel beans, then he couldn't make Kong Yiji act in that teahouse in Lu town.

Of course, if you have a little buddy who wants to experience the feint, you can save this code in a file called Ugly.java:












You can see the results of the program by executing javac Ugly.java from the command line and then the java Ugly command:

Hello world

Once you've experienced it, you can pull it.No one understands writing such code, except machines.Well, my dear reader friend, that's all about this article.Does it feel like the cognitive boundaries have widened again?

I'm Silence King, an interesting programmer.If you find the article helpful, please WeChat to search for "Silence King II" for the first time to read, reply to [666] and more 500G HD instructional videos (categorized) that I have prepared for you.

This paper GitHub Already included, there are complete test points for factory interviews. Welcome to Star.

Originality is not easy. Don't give me a blank ticket. Please give me a compliment for this article. This will be the strongest motivation for me to write more good articles.

Tags: Java REST Programming github

Posted on Tue, 05 May 2020 00:22:14 -0400 by ricta