Detailed explanation of integer in Java that you don’t know
preface
This article mainly introduces the relevant contents of integer in Java for your reference and learning. Let's take a look at the detailed introduction together.
Argument formal parameter
A few days ago, I saw that my circle of friends shared an article "parameter passing mechanism of Java functions -- do you really understand it?"
Some triggers have also studied Java integer before, so write this article, hoping to help you.
exchange
Let's start with an example.
Please use java to complete the swap function and exchange the values of two integer types.
for the first time
If you don't know how Java objects are allocated in memory and how methods pass parameters, you may write the following code.
The results of the run show that the values of a and B are not exchanged.
Let's take a look at the allocation of Java objects in memory when the above program runs:
It can be seen that the local variable tables of the two methods respectively hold references to the actual data addresses of objects a and B.
The swap function implemented above only exchanges the references of local variable a and local variable B in the swap function, and does not exchange the actual data in the JVM heap.
Therefore, the data referenced by a and B in the main function are not exchanged, so the A and B of local variables in the main function will not change.
So how to exchange the data in the main function?
The second time
According to the above practice, you can consider exchanging the data values of a and B on the JVM heap?
Let's briefly understand the object integer. There is only one object-level int type value in it to represent the value of the object.
So we use reflection to modify the value. The code is as follows:
The operation results are in line with expectations.
pleasantly surprised
After the above program runs, if I declare an integer, C = 1, d = 2; What will happen
The sample program is as follows:
The output results are as follows:
Surprise, no surprise! Accident, no accident! Exciting or not!
thorough
What the hell happened? Let's take a look at the decompiled Code:
The author decompiles this directly using ide tools Class file
Integer is used in Java's automatic boxing of primitive type int to integer type Valueof (int) this method.
It must be that this method encapsulates some operations internally, which makes us modify integer Value has a global impact.
All the codes involved in this part are pasted at one time (PS: the author who does not drag is a good code farmer):
As shown above, there is a private static class integercache inside integer, which statically initializes an integer IntegerCache. Low to Java lang.Integer. IntegerCache. Integer array of high.
Where Java lang.Integer. IntegerCache. The value range of high is [127 ~ integer. Max_value - (- low) - 1].
All integers in this interval The object returned by the valueof (int) function is the offset calculated according to the int value, from the array integer IntegerCache. The object obtained from the cache is the same, and no new object will be created.
So when we modify integer After the value of valueof (1), all integers IntegerCache. The return value of cache [1 - integercache. Low] will change.
I believe your IQ should be understood. If you don't, please call 10086 in the comment area.
Well, what about the [integercache. Low ~ integercache. High)?
Obviously, they are lucky that they are not cached by integercache. People outside the law will allocate a piece of land (internal) on the JVM every time they arrive.
Reverie
What if I change the converted parameter to type and int?
With the author's current skills, there is no solution. The master can leave messages on the official account. Thanks a million!
This completes the swap section.
1 + 1
First, let's look at the code:
What is the output?
If you are sure that it is 2, then you have learned in vain. Please dial 95169 directly.
I can definitely tell you that it can be any value in the [integer. Min_value ~ integer. Max_value] interval.
Surprise, no surprise! Accident, no accident! Exciting or not!
Let's roll (stroke) a string (Times) and roast (yards).
The author decompiles this directly using ide tools Class file
The variable two here doesn't call integer Valueof (int) is different from what I thought. I suspect it is the pot of IDE.
So check the compiled bytecode decisively. The following are some of the extracted bytecodes:
It can be seen that it is indeed an IDE pot. Here, integer is called not only once Valueof (int), and also creates an array of objects.
The complete Java code should be as follows:
So just modify integer before the method call IntegerCache. The value of cache [2 + 128] is OK, so add some code in the static initialization part of the class.
two == 2 ?
After modifying integer IntegerCache. After the value of cache [2 + 128], is the variable two still equal to 2?
The above code output is as follows
Because two = = 2 does not involve the conversion of integer boxing or the comparison of the original type, 2 of the original type is always equal to 2.
Integer. The real form of valueof (two) = = 2 is integer valueOf(two). Intvalue = = 2, that is, 3 = = 2, so it is false.
Here you can see that if you compare a null integer variable with an int variable with a double equal sign, you will throw a nullpointexception.
If the method here is changed to system out. What is the output of println ("two =" + two)? You can try.
Postscript
XCache
java. lang.Integer. IntegerCache. high
After looking at the method sun. Com for getting high of the integercache class misc. VM. Getsavedproperty, you may have the following questions. We don't procrastinate and adopt the method of one question and one answer.
1. How is this value passed to the JVM?
As with system properties, when the JVM starts, you can set - DJava lang.Integer. IntegerCache. High = XXX passed in.
2. This method and system What's the difference between getproperty?
In order to distinguish the parameters required by the JVM system from those used by users, Java lang.system. Initializesystemclass saves startup parameters in two places during startup:
2.1 sun. misc. VM. Save all system parameters received by the JVM in savedprops.
At startup, the JVM calls Java lang.system. Initializesystemclass method to initialize the property.
Sun. Is also called misc. VM. Saveandremoveproperties method, from Java lang.System. Remove the following attributes from props:
The properties listed above are system parameters that need to be set for JVM startup. Therefore, for security and isolation reasons, they are set from the system accessible to users Props separate.
2.2 java. lang.System. Props stores parameters other than those required for JVM startup.
PS: JDK 1.8 used by the author 0_ ninety-one
Integercache for Java 9
Imagine that if the above naughty playing methods appear in the third-party dependency package, a group of programmers will go crazy (please don't try such bad playing methods, the consequences are very serious).
Fortunately, Java 9 restricts this. You can write module info in the corresponding module Java file, which restricts the use of reflection to access members. After declaration as required, the code can only access fields, methods and other information that can be accessed by reflection. Only when the class is in the same module or the module opens the package for reflection access. For details, please refer to the following articles:
Modify integercache in Java 9?