Integer與原生類型轉換
Integer提供了幾個與原生類型轉換的方法:
public int intValue() {
return value;
}
public long longValue() {
return (long)value;
}
public float floatValue() {
return (float)value;
}
public double doubleValue() {
return (double)value;
}
由上面的源碼可知,Integer類型轉換原生類型喘先,只需要把value進行對應類型的強制轉換即可,因為Integer中實際存儲的值存儲在value變量中廷粒。
Integer.getInteger(String nm)
getInteger是Integer的靜態(tài)方法窘拯,參數(shù)nm是系統(tǒng)變量key,即從系統(tǒng)變量nm讀取字符串坝茎,然后再轉換為對應的Integer涤姊,此方法有3個重載方法:
public static Integer getInteger(String nm)
public static Integer getInteger(String nm, int val)
public static Integer getInteger(String nm, Integer val)
這三個方法中,后面兩個方法提供了一個默認值嗤放,當nm的系統(tǒng)變量為null思喊,返回方法傳入的默認值val
下面重點分析getInteger(String nm, Integer val)的實現(xiàn)邏輯,因為其他兩個方法的實現(xiàn),都是通過調(diào)用此方法來實現(xiàn)的:
public static Integer getInteger(String nm, Integer val) {
String v = null;
//獲取系統(tǒng)變量nm的值
try {
v = System.getProperty(nm);
} catch (IllegalArgumentException | NullPointerException e) {
}
//當v不為null的時候次酌,調(diào)用Integer.decode(v)方法
if (v != null) {
try {
return Integer.decode(v);
} catch (NumberFormatException e) {
}
}
//如果v==null恨课,返回默認值
return val;
}
程序中使用System.getProperty(nm);獲取的系統(tǒng)變量舆乔,通常是程序,用戶剂公,jvm等相關信息希俩,如果用戶想要設置自定義的系統(tǒng)變量,通掣倭桑可以在啟動程序的時候颜武,使用下面的方法:
java -jar JarName -DpropertyName=value
propertyName就是用戶設置的系統(tǒng)變量的key
下面介紹一下Integer.decode(v)的實現(xiàn)原理
public static Integer decode(String nm) throws NumberFormatException {
//表示進制 8, 10拖吼,16鳞上,默認是10
int radix = 10;
//字符串的當前位置
int index = 0;
boolean negative = false;
Integer result;
if (nm.length() == 0)
throw new NumberFormatException("Zero length string");
//獲取nm的第一個字符,然后判斷是正數(shù)還是負數(shù)
char firstChar = nm.charAt(0);
if (firstChar == '-') {
negative = true;
index++;
} else if (firstChar == '+')
index++;
//然后獲取除了符號位之后的數(shù)字字符串的開頭幾個字符吊档,
//來判斷數(shù)字的進制形式
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
index += 2;
radix = 16;
}
else if (nm.startsWith("#", index)) {
index ++;
radix = 16;
}
else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
index ++;
radix = 8;
}
if (nm.startsWith("-", index) || nm.startsWith("+", index))
throw new NumberFormatException("Sign character in wrong position");
//對字符串經(jīng)過上面步驟的校驗因块,獲取nm代表字符串的進制和數(shù)字字符串,
//通過調(diào)用Integer.valueOf對nm進行真正的字符到數(shù)字的轉換籍铁,
//Integer.valueOf具體實現(xiàn)原理參考:[Integer源碼分析——上 (jdk11)]
try {
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
} catch (NumberFormatException e) {
String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index);
result = Integer.valueOf(constant, radix);
}
return result;
}
注意這里進行了兩步轉換:
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
由于nm.substring(index)是去掉了正負符號和進制類型涡上,因此第一次調(diào)用valueOf得到的是正數(shù)。如果nm表示的負數(shù)拒名,然后需要第二步進行一個正負轉換吩愧。
這里注意的是當拋出NumberFormatException異常是,會執(zhí)行下面兩行代碼:
String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index);
result = Integer.valueOf(constant, radix);
這里的原理呢增显?
當nm="-2147483648"雁佳,在執(zhí)行Integer.valueOf(nm.substring(index), radix)的時候,nm.substring(index)的結果是"2147483648",由于int類型值得范圍是:
-2147483648<=int<=2147483647
而因此在執(zhí)行Integer.valueOf("2147483648", 10)同云,會拋出異常NumberFormatException糖权,因為"2147483648"在轉換的時候出現(xiàn)了溢出,因此需要通過String constant = negative ? ("-" + "2147483648") : "2147483648"; 把"2147483648"轉換為"-2147483648"炸站,然后再調(diào)用Integer.valueOf("-2147483648", radix);
這樣的處理方法可以正確的解決"-2147483648"轉為int的溢出問題星澳。
Integer常用的方法的實現(xiàn)原理就分析到這里,謝謝旱易!