Detailed explanation of cache mechanism principle example of Java wrapper class
This article mainly introduces the cache mechanism principle of Java wrapper class, and explains the example in detail. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
The caching mechanism of Java wrapper class is a function introduced in Java 5 that helps to save memory and improve performance. It is only effective in automatic boxing
Integer wrapper class
Take a chestnut:
Integer a = 127; Integer b = 127; System.out.println(a == b);
The output of this code is true
The underlying implementation of the process of converting a basic type into an encapsulated class object using automatic boxing is to call the valueof method of the encapsulated class:
Integer a =127; Equivalent to integer a = integer valueOf(127);
Take a look at the valueof method of integer:
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
If the input parameter I is greater than or equal to integercache Low or less than or equal to integercache High), get the object from integercache
Take a look at integercache:
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 IntegerCache.high >= 127; } private IntegerCache() {} }
The default range is: - 128 to 127, and the maximum value of the range can be through Java lang.Integer. IntegerCache. Set high to instantiate the data in the range into integer objects and put them into the cache array through the for loop
After testing:
Integer a = 128; Integer b = 128; System.out.println(a == b);
The output result is false, so if the maximum cache value is not specified, the cache will be used when automatic boxing is used between - 128 and 127
Byte wrapper class
Another Chestnut:
public static void main(String[] args) { Byte a = 127; Byte b = 127; System.out.println(a == b); //true }
Since byte ranges from - 128 to 127, the valueof of byte is obtained from the bytecache cache
public static Byte valueOf(byte b) { final int offset = 128; return ByteCache.cache[(int)b + offset]; }
Bytecache class:
private static class ByteCache { private ByteCache(){} static final Byte cache[] = new Byte[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Byte((byte)(i - 128)); } }
Compared with integercache, the maximum value of bytecache cannot be modified, which is 127
Short packaging class
public static Short valueOf(short s) { final int offset = 128; int sAsInt = s; if (sAsInt >= -128 && sAsInt <= 127) { // must cache return ShortCache.cache[sAsInt + offset]; } return new Short(s); }
Shortcache class:
private static class ShortCache { private ShortCache(){} static final Short cache[] = new Short[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Short((short)(i - 128)); } }
The maximum value of shortcache cannot be modified. The range can only be - 128 ~ 127
The valueof method and longcache class of long wrapper class are consistent with the implementation of short wrapper class, and the range can only be - 128 ~ 127
Character wrapper class
Valueof method:
public static Character valueOf(char c) { if (c <= 127) { // must cache return CharacterCache.cache[(int)c]; } return new Character(c); }
Charactercache class:
private static class CharacterCache { private CharacterCache(){} static final Character cache[] = new Character[127 + 1]; static { for (int i = 0; i < cache.length; i++) cache[i] = new Character((char)i); } }
The cache range of character is between 0 and 127
Boolean wrapper class
Valueof method:
public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }
True and false are static variables modified by static final
public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false);
Float wrapper class & double wrapper class
Valueof method:
public static Float valueOf(float f) { return new Float(f); } public static Double valueOf(double d) { return new Double(d); }
Float and double do not use cache, but directly use new objects
Summary:
Java wrapper classes: byte, short, integer, long, character use static code blocks to initialize the cache, where the maximum value of integer can be through Java lang.Integer. IntegerCache. High setting; Boolean uses static final to instantiate the object; Float and double direct new objects do not use caching
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.