java 包装类方法总结_【源码】java包装类总结
1.包裝類除了Void和Character,其他六個全部都繼承自Number。Number是一個抽象類。如下:
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.Integer、Byte、Short、Long內部都有"緩存"靜態類,以Integer為例:
緩存即一個Integer數組,默認大小為-128~127。初始化時,會new出256個Integer對象存在此數組中。
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() {}
}
當調用Integer.valueOf方法時,會首先判斷參數是否在緩存范圍內,若在,直接返回緩存對象,否則,構造之。
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);
}
可見,Integer將加載緩存數組延遲到了最晚,避免浪費空間。
另外可以通過intValue方法返回Integer對應的int值(其他包裝類類似):
public int intValue() {
return value;
}
value是Integer成員變量,構造時被傳遞進來:
private final int value;
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}3.除了Void類,其余包裝類均實現了Comparable接口,可以通過compareTo方法比較兩個包裝類的大小。同時包裝類提供了靜態的compare方法比較兩個參數的大小(以Integer為例):public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
compareTo方法其實調用了compare靜態方法:public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}4.Integer的getInteger方法的作用是返回具有指定名稱的系統屬性的整數值,不要誤用。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.包裝類的toString方法經過了重寫
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.可以使用parseXXX方法將字符串轉化為對應的數字。
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);
}
原文:http://blog.csdn.net/chdjj/article/details/38356325
總結
以上是生活随笔為你收集整理的java 包装类方法总结_【源码】java包装类总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宝塔 ddos(宝塔防御ddos攻击方法
- 下一篇: 升级iOS 16后如何通过LTE进行网络