Notes and interview questions on the use of integer based on Java
Reference of integer object in Java
There is no pointer in Java, but there is also the concept of reference. What I want to say here is whether integer is the same object.
1. Let's start with a code:
result:
b1=5000
c1=5000
From the above, first of all, here are a few,
1) For integer, the integer between - 128-127 has been initialized and placed in the integercache. If it is boxed, the object will be taken from it.
2) Is B1 = A1 a numeric assignment or the same object? From the actual results, we can see that B1 and A1 point to the same object, not the same value
3) , C1 = 100, indicating that the values between - 128-127 are objects obtained from integercache. After the integer object corresponding to 100 is changed, the subsequent boxing of 100 is changed. Because the objects in the cache are obtained by array index instead of numerical comparison.
However, modifying this cache will be more dangerous. I don't mind. Who knows what jar package or platform to pack 100, but the result is not 100, and then it will crash.
2. Through the above description, what is the answer if it is changed to this
3. Then change it again
What's the answer. For the new operation, objects are generated in the heap instead of being boxed.
It is not difficult to understand boxing, caching and reference. You can try it yourself.
Let's start with some basic knowledge
Among the above eight basic data types, only int - > integer char - > character change greatly, and the rest only convert the initial letter to lowercase. Let's take a look at the new features of jdk5: automatic packing and unpacking
Auto packing: convert basic type to packing type
Automatic unpacking: convert the packing type to the basic type
matters needing attention
Interview questions
reason:
New opens up space in the heap memory. Naturally, the comparison address values (= =) are all false
Since integer overrides the equals method, the equals output is true
You may feel that it is too simple without any technical content, because the above is not the focus. See the code below
reason:
Why are two objects when int is greater than 127? Do you feel familiar with the number 127?
-128 to 127 are the value range of byte. If it is within this value range, automatic boxing will not create a new object, but get it from the constant pool
If the value range of byte is exceeded, a new object will be created
The bottom layer of automatic boxing will call the valueof () method. Simple source code analysis (JDK1.8):
8 basic types of wrapper classes and object pools
Most of the basic types of wrapper classes in Java implement constant pool technology. These classes are byte, short, integer, long, character and Boolean, while the other two types of floating-point wrapper classes are not implemented. In addition, the five integer wrapper classes byte and character can only use the object pool when the corresponding value is less than or equal to 127, that is, the object is not responsible for creating and managing the objects of these classes greater than 127
Expand knowledge
In the JVM specification, each type has its own constant pool. A constant pool is an ordered collection of constants used by a type, It includes direct constants (basic types, string) and symbolic references to other types, fields and methods. The reason why it is a symbolic reference rather than specifying other types directly at compile time, as in C language, is because Java is dynamically bound. The specific dependent type instances can be determined only according to some rules at run time, which is the basis of Java polymorphism.
In the JVM, the whole life cycle of a class from being loaded into the virtual machine memory to unloading the memory includes seven stages: loading, verification, preparation, parsing, initialization, use and unloading. The parsing phase is the process in which the virtual machine replaces the symbolic reference in the constant pool with a direct reference.
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.