Traps encountered in Integer type parameter transfer at work

Problems encountered

//i = 0
Integer integer=new new Integer(i); 
//i改成4
tempList = this.commonCalMainProcess(list,indexList,integer,position,null); 
//i还是等于0
i = integer;

Misunderstanding: The object passed is the reference of the address, so if the value of the integer is changed, the value it points to will also change.

A unit test

@Test
    public void test02(){
    
    
        Integer a1 = Integer.valueOf(60);
        Integer b1 = 60;
        System.out.println(System.identityHashCode(a1));
        System.out.println(System.identityHashCode(b1));
        System.out.println("1:"+(a1 == b1));
        System.out.println("------------------");
        Integer a2 = 60;
        Integer b2 = 60;
        System.out.println(System.identityHashCode(a2));
        System.out.println(System.identityHashCode(b2));
        System.out.println("2:"+(a2 == b2));
        System.out.println("------------------");
        Integer a3 = new Integer(60);       //a3--->堆里的地址,常量池中也会有一个60
        Integer b3 = 60;                          //b3---->常量池中的60
        System.out.println(System.identityHashCode(a3));
        System.out.println(System.identityHashCode(b3));
        System.out.println("3:"+(a3 == b3));
        System.out.println("------------------");
        Integer a4 = 127;
        Integer b4 = 127;
        System.out.println(System.identityHashCode(a4));
        System.out.println(System.identityHashCode(b4));
        System.out.println("4:"+(a4 == b4));
        System.out.println("------------------");
        Integer a5 = 128;
        Integer b5 = 128;
        System.out.println(System.identityHashCode(a5));
        System.out.println(System.identityHashCode(b5));
        System.out.println("5:"+(a5 == b5));

    }

Output:
Insert picture description here
From the example above, we can learn:

  1. Integer i = new Integer(num); In fact, the object is created twice, the address of one object is placed in the heap and the other is placed in the constant pool, for example: the output of 3 above
  2. If the return of i is within -127~128, this object can be placed in the constant pool, so if the defined Integer i = in this range, directly refer to its address, in fact the same one; but if it exceeds this return, it will be Re-create new objects in the constant pool, for example: the output of 4 and 5 above
  3. Integer a2 = 60 is actually Integer a2 = Integer.valueOf(60), automatic boxing operation, within -127~128, if there is this object in the constant pool, they refer to the object of the constant pool.

Through the above examples, you can explain the problems encountered

Integer points to an object in the heap, and i also has a corresponding object in the constant pool, so when we pass integer to the function and change it, the modification is actually the value corresponding to its address in the constant pool, so Does not cause a real change in integer, so it is still 0! !

solution

If you must modify the value of this integer through a function, there are the following solutions:

  1. The changed value integer uses the return value of the function to receive integer = fun(); the value obtained is the modified value. This method depends on the specific business scenario.

  2. Encapsulate a class yourself to class MutableInteger{ int value;}deal with the troublesome points

  3. Use AtomicIntegeratomic reshaping of objects

    AtomicInteger integer=new AtomicInteger(i);
    tempList = this.commonCalMainProcess(list,indexList,integer,position,null);
    int i = integer.intValue();
    
    void commonCalMainProcess(...,AtomicInteger ai...,){
          
          
    		ai.set(value);
    }
    

Guess you like

Origin blog.csdn.net/JAYU_37/article/details/109484159