I shared a post on my blog about comments in Java, and I swore: “Comments are not executed!” “Teacher, are you sure?

I have always had an advantage, that is, I can listen to the voice of others, whether you praise or criticism, is always open to accept. Because I believe that most of you have good intentions.

Besides, I never really wanted to be a tech giant. I just like to share. Many of the mistakes I made in the articles I kept intact, because if I corrected them, the people who pointed out the mistakes in the comments would look out of place in the eyes of later readers.

Diss my friends, rest assured, I will not mind.

Nevertheless, but for annotation this matter, really can’t bear ah! Comments will not be executed. I think this is sarcasm. So I emailed him to ask why, and he threw me the following code:

public class Test {

    public static void main(String[] args) {

        String name = "Silent King II.";

        // \u000dname=" silent King three ";

        System.out.println(name);

    }

}

Copy the code

I copied it to IDEA and ran it. The output result of the program was beyond my expectation:

The king of silent three

Copy the code

It was king Three, not king two. When I saw this result, I was completely stunned.

At that moment, I felt that I had learned Java for ten years. In college, the teacher said that comments were not implemented; Even Programming Ideas says that comments are not executed. Now who can tell me why?

Isn’t the world of programming simple? It’s either 0 or 1, right? At this point, we have to study it carefully.

From a pure code point of view, the problem should be the special string of characters – \ u000D, if it is not the problem, the value of name changed from “King of Silence 2” to “King of Silence 3”, there is no other reason – nothing else, with years of work experience, I am very good at finding the root of the problem.

\u000d looks strange, but I know it is a Unicode character. Ask the search engine and learn that it represents a line break – an aha! I know that the Java compiler not only compiles code, but also parses Unicode characters.

I took a quick look at the compiled bytecode of the above code and it looked something 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

   L0

    LINENUMBER 3 L0

    ALOAD 0

    INVOKESPECIAL java/lang/Object.<init> ()V

    RETURN

   L1

    LOCALVARIABLE this Lcom/cmower/dzone/secret/Test; L0 L1 0

    MAXSTACK = 1

    MAXLOCALS = 1



  // access flags 0x9

  public static main([Ljava/lang/String;) V

   L0

    LINENUMBER 5 L0

    LDC "\u6c89\u9ed8\u738b\u4e8c"

    ASTORE 1

   L1

    LINENUMBER 6 L1

    LDC "\u6c89\u9ed8\u738b\u4e09"

    ASTORE 1

   L2

    LINENUMBER 7 L2

    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;

    ALOAD 1

    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;) V

   L3

    LINENUMBER 8 L3

    RETURN

   L4

    LOCALVARIABLE args [Ljava/lang/String; L0 L4 0

    LOCALVARIABLE name Ljava/lang/String; L1 L4 1

    MAXSTACK = 2

    MAXLOCALS = 2

}

Copy the code

Well, it means you don’t understand. It doesn’t matter, just decompile it, so I see this code:

public class Test {

    public Test(a) {

    }



    public static void main(String[] args) {

        String name = "Silent King II.";

        name = "Silent King three.";

        System.out.println(name);

    }

}

Copy the code

Well, the two backslashes // are missing, and that’s for sure — comments don’t actually execute. Only \u000d put name=” Silent King three “; To the next line of the // comment, it looks like this:

public class Test {

    public static void main(String[] args) {

        String name = "Silent King II.";

        //

        name="Silent King three.";

        System.out.println(name);

    }

}

Copy the code

Is this a Java bug? It doesn’t count.

Because by allowing Java source code to include Unicode characters, you can ensure that code written in any region of the world will be executed elsewhere.

To be honest, I found this paragraph from the Internet, as if I understand something, but also as if I don’t understand. Let’s look at another piece of code:

doublePI = Math. PI;

System.out.println(\u03C0);

Copy the code

If programmer Wang didn’t know how to type π when he created the periodic rate variable, he could have used \u03C0 instead — the compiler knows \u03C0 is π (the compiler parses Unicode characters before compiling any other code).

It can only be said that \ u000D is an exception.

Of course, except in special cases, do not include Unicode characters in your source code to avoid changing the original meaning of the source code.

I don’t want to delve into anything too esoteric in this article, just to raise the awareness that annotations can be executed by compilers. Just like, lu Xun couldn’t get Kong Yiji to dress up as a forced animal in the teahouse in Lu Town if he didn’t know that there are four ways to write hui in beans flavored with anise.

Of course, if you want to get a taste of that, you can save this code in a file called ugly.java:

\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

Copy the code

From the command line, execute javac ugly. Java and then Java Ugly to see the result of the program:

Hello world

Copy the code

After the experience, just pull it. Writing code like this wouldn’t make sense to anyone except a machine. Well, my dear readers, that’s all for this article. Is the boundary of sensory cognition widened again?

I am silent King 2, an interesting programmer. If you think this article is helpful to you, please search “Silent King ii” on wechat and read it for the first time. Reply [666] there is also a 500G HIGH-DEFINITION teaching video (classified) that I prepared for you.

Interview on GitHub

Original is not easy, do not want a free ticket, please point a praise for this article, it will be the strongest power FOR me to write more high-quality articles.