The java Grammar Sugar - ----------------------------------- Auto-packing and Unpacking

Posted by unerd.co.uk on Mon, 07 Oct 2019 23:12:00 +0200

I. Sample Code

   @Test
   public  void test3(){
       int i=1;
       int j=i++; 
       if( i==(++j)&&( i++) == j){
               i+=j;
       }
       System.out.println("i'"+ i);
   }
  • The results are as follows:5

  • So why does that happen? Look closely

2. Auto-loading and disassembling

  • Definition

    • Auto-packing, after compiling, is transformed into the corresponding packaging method, Integer.valueOf ();

    Integer i =1; executed in compilation: Integer i= Integer.valueOf(int i);

    • After compilation, it is converted to the corresponding restore method, Integer.intValue () method.

    int j = i++; perform unboxing in compilation, int j = Integer.inValue(i);

  • The example calls unboxing. The constant pool in the java virtual machine includes Integer constant pool, which is stored in a table. The details are as follows:

The stored range value defaults to - 128 - 127, so if Interger points to the number in this range, it will directly point to the object in the constant pool at compilation time.

3. Comparing addresses in two ways:

  • First kind

    System.out.println(System.identityHashCode(i));
    System.out.println(System.identityHashCode(i++));
    System.out.println(System.identityHashCode(++j));
       
  • Second kinds

    public class javaAuto {
        static final Unsafe unsafe = getUnsafe();
        static final boolean is64bit = true;
    
        public static void main(String... args) {
       
       int i=1;
       int j=i++;
       System.out.println("-------------------------");
       System.out.println("----");
       printAddresses("i", i);
    
       System.gc();
       System.out.println("----");
       printAddresses("j", ++j);
       
       System.gc();
       System.out.println("----");
       printAddresses("i++", i++);
        }
    
        public static void printAddresses(String label, Object... objects) {
       System.out.print(label + ": 0x");
       long last = 0;
       int offset = unsafe.arrayBaseOffset(objects.getClass());
       int scale = unsafe.arrayIndexScale(objects.getClass());
       switch (scale) {
           case 4:
               long factor = is64bit ? 8 : 1;
               final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
               System.out.print(Long.toHexString(i1));
               last = i1;
               for (int i = 1; i < objects.length; i++) {
                   final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
                   if (i2 > last)
                       System.out.print(", +" + Long.toHexString(i2 - last));
                   else
                       System.out.print(", -" + Long.toHexString( last - i2));
                   last = i2;
               }
               break;
           case 8:
               throw new AssertionError("Not supported");
       }
       System.out.println();
        }
    
        private static Unsafe getUnsafe() {
       try {
           Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
           theUnsafe.setAccessible(true);
           return (Unsafe) theUnsafe.get(null);
       } catch (Exception e) {
           throw new AssertionError(e);
       }
        }
    
    }
    
  • The addresses of i, i++, ++ J printed in both ways are the same; what are their values?

Four, value

  • The values of i and + + j are both 2, which is beyond doubt.
  • (i++)==j, this comparison, the landlord suspects is the first to perform the comparison, and then to perform the + + operation, but it does not seem to conform to the operation rules of operators.
  • To be checked... I would appreciate it if someone could tell me the answer.

Reference link: https://blog.csdn.net/isscollege/article/details/78398968

Welcome to the Public Number for more information:

Topics: Java