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...

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";
System.out.println(name);
}
}

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
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
}

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";
System.out.println(name);
}
}

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";
System.out.println(name);
}
}

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;
System.out.println(\u03C0);

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:

\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

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.

5 May 2020, 00:22 | Views: 5179

Add new comment

For adding a comment, please log in
or create account

0 comments