== Java learning> int and Integer differences and connections

A difference

1, type

  • int is one of the original eight basic java data types;
  • Integer is a class that provides a lot of cosmetic packaging daily operations;

2, the storage location and size

  • int jvm is provided by the bottom, by the Java Virtual Machine Specification, int-type data stored in the local variables, occupies one data unit;
  • Heap Integer Data stored in the Java runtime data area, may be garbage collection mechanism is not in use;
    • Integer object takes up storage space:
      • Mark Word: 4 bytes, flag;
      • Object Pointer Class: 4 bytes, point to the memory address corresponding to the object class;
      • Alignment: alignment padding bytes, padding with eight bytes;
      • 4 + 4 + 8 = 16 bytes;

3, the difference between bytes of used codes

  • int byte-code example:
    • Definitions: int num1 = 32;
    • Byte code: 0: bipush 32;
  • An Integer Byte Code Example:
    • Defined: Integer num2 = 64;
    • Byte code:
      • 3: bipush 64;
      • 5:invokestatic #20 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;

Second, contact

1, interconversion

  • Integer.valueOf, int -> Integer;
  • Integer.intValue() Integer -> int;
  • It can also be assigned directly to each other;

2, the automatic entry box operation (auto boxing / unboxing)

  • int -> Integer, packing;
  • Integer -> int, unpacking;
  • Note: the program to avoid inadvertent operation of the entry box, especially when there are performance considerations;

3, automatic packing (int -> Integer) Analysis caching mechanism

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

  static {
    // high value may be configured by property
    int h = 127;
    String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    if (integerCacheHighPropValue != null) {
      try {
        int i = parseInt(integerCacheHighPropValue);
        i = Math.max(i, 127);
        // Maximum array size is Integer.MAX_VALUE
        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
      } catch( NumberFormatException nfe) {
        // If the property cannot be parsed into an int, ignore it.
      }
    }
    high = h;

    cache = new Integer[(high - low) + 1];
    int j = low;
    for(int k = 0; k < cache.length; k++)
      cache[k] = new Integer(j++);

    // range [-128, 127] must be interned (JLS7 5.1.7)
    assert Integer.IntegerCache.high >= 127;
  }

  private IntegerCache() {}
}
IntegerCache

  Integer valueOf automatic packing operation will call () method, found by the above source code analysis: first determines the numerical ranges int type in the call valueOf () method, if the buffer Integer range, the call IntegerCache class, if Integer is not within the scope of the cache, the new direct a new Integer object.

  Look IntegerCache source, found Integer Default cache minimum range is -128, the first parameter will be to read the JVM configured in getting maximum value, if the value of the parameter configuration, the take this value and compare whichever 128 Integer greater value as the maximum value of the cache, if the parameter is not configured, then take the maximum value 128 as an Integer.

  Now we come to some a way to prove the above analysis, the following code:

public void integerCache() {
  Integer i1 = 128;
  Integer i2 = 128;

  // false
  System.out.println(i1 == i2);

  Integer i3 = 127;
  Integer i4 = 127;

  // true
  System.out.println(i3 == i4);

  Integer i5 = 257;
  Integer i6 = 257;

  // false
  System.out.println(i5 == i6);
}

The operation method was as follows:

Range in the buffer 127, and 128 and 257 are no longer in the buffer range.

We add JVM configuration -XX: AutoBoxCacheMax running again after = 256 which results are as follows:

 

Proof configuration takes effect, 127 and 128 in the buffer range, the buffer 257 is no longer within range. These results prove that our analysis of the source code is correct.

Precautions:

  • In doing equal determination, if it is the original type == can be used directly, if it is the equals type of packaging is required;
  • If you use the type of packaging business, pay attention to the default value is null, because the default values ​​of the original data type int is 0 easy for us to develop a habit, here is the most easy to neglect;

4, expansion

 

Guess you like

Origin www.cnblogs.com/L-Test/p/11440827.html