This is the second day of my participation in Gwen Challenge
-
Basic types of caching mechanisms, design patterns
Integer The default cache is between -128 and 127
Boolean, caches true/false for instances, exactly, only two constant instances will be returned. Boolean.TRUE/FALSE
Short, also caches values between -128 and 127
Byte, values are limited, so all are cached
Character, cache range ‘\u0000’ to ‘\u007F’
The cache upper limit can actually be adjusted as needed, and the JVM provides a parameter setting: -xx :AutoBoxCacheMax=N
You can look at the cache for the Integer internal class IntegerCache as follows
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() {} public static Integer valueOf(int I) {if (I >= integerCache.low && I <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }Copy the code
Java objects are reference types. If it’s an array of primitive data types, it’s a contiguous chunk of memory in memory, whereas an array of objects is not. Data is stored by reference, and objects tend to be scattered around the heap. While this design allows for great flexibility, it also leads to inefficiencies in data manipulation, especially when modern CPU caching mechanisms are not fully utilized.
-
What’s the difference between a deep copy and a shallow copy?
Object A copies object B. Modify the value and String of the base type 8 (including boxing) in object B. Does not change in object A. repair
Change object a reference object in object B. Reference objects in object A also change.
A deep copy. Object A is A copy of object B. Modify eight basic data types in object B. And reference objects. The value in object A is not modified.
Including only primitive data types and immutable classes does not affect independence.
Shallow copy for mutable reference properties only copies the reference, the original object and the copied object, not independent of each other.
Deep copy also creates a new object for a variable reference property, and the resulting copy object is completely independent.
-
Generics can’t use primitive data types. Why?
Generics in Java are a fully compile-time construct – the compiler converts all generic uses to the correct type. This is to maintain backward compatibility with previous JVM runtimes.
This:
List<ClassA> list = new ArrayList<ClassA>(); list.add(new ClassA()); ClassA a = list.get(0); Copy the code
Become (roughly) :
List list = new ArrayList(); list.add(new ClassA()); ClassA a = (ClassA)list.get(0); Copy the code
Therefore, anything used as a generic must be convertible to Object (in this example get(0) returns Object), whereas primitive types are not. Therefore, they cannot be used for generics.
-
Switch – Case Indicates the supported type
Byte,short,char,int,enum(Java 7),String(6), wrapper class is not supported, but the following code is correct again
Switch (new Integer(33)){// Pass, because automatic fit-out and unpacking case 43: break; Case Integer.valueof (88): // Error break; }Copy the code
w