Deep analysis of Java disassembly box

Let's start with a code:

Guess what?

Many people will think that the results are all true, but that's not the case

true false

Why is this the result? If memory is used to interpret the results, num1 and num2 point to the same object, while Num3 and num4 point to different objects. Next, I'll tell you why. Take a look at the source code of the valueof method of integer type:

The implementation of integercache:

It can be seen from these two codes that when creating an integer type object through the valueof method, the value range is [- 128127], the value is in this range, and the pointer points to integercache For the existing object reference in the cache, when the value exceeds this range, a new object will be created.

It should be noted that not all types are in this range. Look at the double type:

Final output:

false false

Specifically, why is this result? We can look at the implementation of the double valueof method in the source code. It is different from the integer valueof method because the number of integer values in a certain range is limited, but the floating point number is not.

Note that the implementation of valueof methods of integer, short, byte, character and long is similar. The implementation of the valueof method of double and float is similar.

One is pulled down. The result of boolean type has two true or false. Look directly at the source code:

True and false are defined as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>