当前位置:网站首页>Learn to record and analyze

Learn to record and analyze

2020-11-08 14:34:00 cw_T

Learn to record and analyze

Record the difficulties in learning , A little white !

I have not read the source code , Pure Xiaobai , I wrote this article just to improve myself !!! I hope you can point out the mistakes .

Curious, I looked through Integer.valueOf() Method source code

public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high)
         return IntegerCache.cache[i + (-IntegerCache.low)];
     return new Integer(i);
 }

First of all, the parameters explain :

static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;

high No assignment , So we continue to track down high attribute

 static {
     int h = 127;
     // If you specify -Djava.lang.Integer.IntegerCache.high=XXX( Enter your own data ) Parameters 
     // The following statement will be executed , Then dynamic settings h yes 127 Or set the input data 
     String integerCacheHighPropValue =
         // The method is by passing a key, Get the value set manually at startup 
         VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
     if (integerCacheHighPropValue != null) {
         try { 
            
             h = Math.max(parseInt(integerCacheHighPropValue), 127);
         	 //	
             // public static int max(int a, int b) {
      		//	  return (a >= b) ? a : b;
    		//	}
             
             // Maximum array size is Integer.MAX_VALUE
             //32 Bit system 2147483647=2^32
             h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
            
         } catch( NumberFormatException nfe) {
             // If the property cannot be parsed into an int, ignore it.
         }
     }
   	// find high The value of the property is determined by h Local attribute assignment 
     high = h;

     // Load IntegerCache.archivedCache from archive, if possible
     VM.initializeFromArchive(IntegerCache.class);
     
     int size = (high - low) + 1;// Can be saved 256 Number .
     // As for why 256? I'm not going to talk about , Suggest yourself to search the computer storage data related concepts !

     // Use the archived cache if it exists and is large enough
     if (archivedCache == null || size > archivedCache.length) {
         Integer[] c = new Integer[size];
         int j = low;
        //c.length=256
        //j=-128
         for(int i = 0; i < c.length; i++) {
             c[i] = new Integer(j++);
         }
         // Array c Corresponding 256 Number ,c[0] Corresponding -128,c[256] Corresponding 127
         // It's clever 
         archivedCache = c;
     }
     cache = archivedCache;
     // range [-128, 127] must be interned (JLS7 5.1.7)
     // Assertion , If the maximum value is less than 127 Throw an exception 
     assert IntegerCache.high >= 127;
 }


Now let's go back to

public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high)
         // As mentioned before, the design is very clever 
         //i Why add -(-128)? Do you understand !
         // The reason is because cache The first of an array of 0 Bit save is -128!
         return IntegerCache.cache[i + (-IntegerCache.low)];
     return new Integer(i);// If it's not in that range , Then create an instance 
 }

Now we see that if the argument is in -128 To 127 Between , It will directly take the instance object from the static method area , This is done to reduce the cost .

The following validation

Integer int_instance1 = Integer.valueOf("100");
Integer int_instance2 = Integer.valueOf("100");
System.out.println(int_instance1 == int_instance2);

The result of the operation is true


Integer int_instance1 = Integer.valueOf("100");
Integer int_instance2 = Integer.valueOf("101");
System.out.println(int_instance1 == int_instance2);

The result of the operation is false

The reasons are explained in the following table , A simple drawing

Integer int_instance1 = Integer.valueOf("128");
Integer int_instance2 = Integer.valueOf("128");
System.out.println(int_instance1 == int_instance2);

The result is false

版权声明
本文为[cw_T]所创,转载请带上原文链接,感谢