[Source code] java packaging class summary, source code java Packaging
1. Except for Void and Character, all the other six classes are inherited from Number. Number is an abstract class. As follows:
public abstract class Number implements java.io.Serializable { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } private static final long serialVersionUID = -8742448824652078965L;}
2. There are "cache" static classes in Integer, Byte, Short, and Long. Taking Integer as an example:
The cache is an Integer array. The default size is-128 ~ 127. During initialization, 256 new Integer objects are stored in this array.
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low)); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }
When the Integer. valueOf method is called, the system first checks whether the parameter is within the cache range. If so, the system directly returns the cached object. Otherwise, the system constructs the parameter.
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
It can be seen that Integer delays loading the cache array to the latest level to avoid space waste. In addition, you can use the intValue method to return the int value corresponding to an Integer (similar to other packaging classes ):
public int intValue() { return value; }
Value is an Integer member variable, which is passed in during construction:
private final int value; public Integer(int value) { this.value = value; } public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
3. In addition to the Void class, other packaging classes all implement the Comparable interface. You can use the compareTo method to compare the sizes of the two packaging classes. At the same time, the packaging class provides a static compare method to compare the sizes of two parameters (taking Integer as an example ):
public int compareTo(Integer anotherInteger) { return compare(this.value, anotherInteger.value); }
The compareTo method actually calls the compare static method:
public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); }
4. The getInteger method of Integer is used to return the Integer of the system attribute with the specified name. Do not misuse it.
public static Integer getInteger(String nm, Integer val) { String v = null; try { v = System.getProperty(nm); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } if (v != null) { try { return Integer.decode(v); } catch (NumberFormatException e) { } } return val; }
5. The toString method of the packaging class has been rewritten.
public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true); }public String toString() { return toString(value); }
6. You can use the parseXXX method to convert a string to a corresponding number.
public static int parseInt(String s, int radix) throws NumberFormatException { /* * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */ if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; } public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
Java packaging problems
= Is used to compare whether the values of two basic data types are equal, = is also used to determine whether two object reference names refer to the same object. For values between-128 and 127 during automatic packing, if they are packed as Integer objects, they will be reused in memory (that is, the memory points to the same ).
Therefore, System. out. println (i1 = i2) is output to true.
If the value range from-128 to 127 is exceeded
It is not reused (pointing to a new memory address), that is, it is equivalent to creating an Integer object each time it is packed.
So System. out. println (i3 = i4); Output false
Conclusion: The value between-128 and 127 refers to the same address in the memory space, and a new memory space exists if the value exceeds.
Java Packaging
Yes. Any method you can use is a method that java has encapsulated in the jar package. You can use it directly.
Java is open-source. If you want to view the source code, press and hold the ctrl key, move the cursor over the corresponding method, and click it to view it;
If it is an interface, you can see two options according to the above operations. One is Open Declaration and Open Implementation. The former is the interface definition, and the other is the interface Implementation. You can click this point in System. out. test the println () method in println (I) by yourself.