What are the differences in how Java basic types are stored as local variables and member variables?
世界只因有你
世界只因有你 2017-05-27 17:41:41
0
1
547

1. This question may involve many aspects. I studied it myself and understood part of it, but some parts are still unclear. Paste the code first (Java version 1.8):

public class Test{
    int abc1 = 127;
    Integer abd1 = 127;
    Integer abf1 = 127;
    Integer abe1 = new Integer(127);
    {
        System.out.print("1\t");
        System.out.println(abc1==abd1);
        System.out.print("2\t");
        System.out.println(abd1==abe1);
        System.out.print("3\t");
        System.out.println(abc1==abe1);
        System.out.print("4\t");
        System.out.println(abd1==abf1);
    }

    int abc2 = 128;
    Integer abd2 = 128;
    Integer abf2 = 128;
    Integer abe2 = new Integer(128);
    {
        System.out.print("5\t");
        System.out.println(abc2==abd2);
        System.out.print("6\t");
        System.out.println(abd2==abe2);
        System.out.print("7\t");
        System.out.println(abc2==abe2);
        System.out.print("8\t");
        System.out.println(abd2==abf2);
    }

    public static void main(String[] args){
        Test t =new Test();
    }
/*输出为:
1       true
2       false
3       true
4       true
5       true
6       false
7       true
8       false
*/
}

2. Let me talk about the part that I know clearly first: the 4th output and the 8th output are relatively clear. This is because there is a constant pool in the Java heap for storing commonly used basic data type literals. This constant pool can store integers (-128 to 127) and Boolean types (no double types). When executing "Integer abd1=127", in addition to creating an Integer object with a value of 127 in the heap, it will also store 127 in the corresponding constant pool, and then associate this Integer object with 127 in the constant pool; When "Integer abf1=127" is executed again, in addition to creating the object, it is also associated with 127 in the constant pool, so comparing the two returns true. 128 is different. Since it exceeds the storage range of the constant pool, only two Integer references i1 and i2 are compared, so false is returned.

3. My question is: How to store the int type (non-static, non-final) in object member variables. In other words, when a Text object t is created, is abc1 (abc2 similar to this) stored directly on the stack or packaged and stored in the heap? Why do 1-3 (or 5-7) appear? The return is "true, false, true" situation.

世界只因有你
世界只因有你

reply all(1)
给我你的怀抱

When an int is compared with an Integer, the Integer will be automatically unboxed and compared with an int.
The instance variable of the second object is allocated on the heap.
1 and 5 are compared because the Integer type is automatically unboxed, so it is true.
new Integer(xxx) xxx is even in the cache. New objects will also be created within the scope, so 2 is false

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!