日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 包装类方法总结_【源码】java包装类总结

發(fā)布時(shí)間:2023/12/2 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 包装类方法总结_【源码】java包装类总结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.包裝類(lèi)除了Void和Character,其他六個(gè)全部都繼承自Number。Number是一個(gè)抽象類(lèi)。如下:

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內(nèi)部都有"緩存"靜態(tài)類(lèi),以Integer為例:

緩存即一個(gè)Integer數(shù)組,默認(rèn)大小為-128~127。初始化時(shí),會(huì)new出256個(gè)Integer對(duì)象存在此數(shù)組中。

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() {}

}

當(dāng)調(diào)用Integer.valueOf方法時(shí),會(huì)首先判斷參數(shù)是否在緩存范圍內(nèi),若在,直接返回緩存對(duì)象,否則,構(gòu)造之。

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);

}

可見(jiàn),Integer將加載緩存數(shù)組延遲到了最晚,避免浪費(fèi)空間。

另外可以通過(guò)intValue方法返回Integer對(duì)應(yīng)的int值(其他包裝類(lèi)類(lèi)似):

public int intValue() {

return value;

}

value是Integer成員變量,構(gòu)造時(shí)被傳遞進(jìn)來(lái):

private final int value;

public Integer(int value) {

this.value = value;

}

public Integer(String s) throws NumberFormatException {

this.value = parseInt(s, 10);

}3.除了Void類(lèi),其余包裝類(lèi)均實(shí)現(xiàn)了Comparable接口,可以通過(guò)compareTo方法比較兩個(gè)包裝類(lèi)的大小。同時(shí)包裝類(lèi)提供了靜態(tài)的compare方法比較兩個(gè)參數(shù)的大小(以Integer為例):public int compareTo(Integer anotherInteger) {

return compare(this.value, anotherInteger.value);

}

compareTo方法其實(shí)調(diào)用了compare靜態(tài)方法:public static int compare(int x, int y) {

return (x < y) ? -1 : ((x == y) ? 0 : 1);

}4.Integer的getInteger方法的作用是返回具有指定名稱(chēng)的系統(tǒng)屬性的整數(shù)值,不要誤用。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.包裝類(lèi)的toString方法經(jīng)過(guò)了重寫(xiě)

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方法將字符串轉(zhuǎn)化為對(duì)應(yīng)的數(shù)字。

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

總結(jié)

以上是生活随笔為你收集整理的java 包装类方法总结_【源码】java包装类总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。