欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java類型轉(zhuǎn)換valueOf與parseInt區(qū)別探討解析

 更新時間:2022年09月27日 11:26:55   作者:????????????  
這篇文章主要為大家介紹了Java類型轉(zhuǎn)換valueOf與parseInt區(qū)別探討解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

在編程中,遇到類型轉(zhuǎn)換,好像會經(jīng)常用到 parseIntvalueOf,當然這里只拿 Integer 類型進行陳述,其他類型也是雷同的;

想必有讀者也跟我一樣,經(jīng)常交叉使用這兩個方法,但卻不知道這兩者到底有什么區(qū)別,接下來就來探究一番;  

區(qū)別

  • Integer.parseInt(s) 的作用就是把字符串 s 解析成有符號基本類型的 int;
  • Integer.valueOf(s) 把字符串 s 解析成 Integer 對象類型,返回的對象可以調(diào)用 Integer 中的方法;

接下來,通過源碼進行逐一解析;  

parseInt

我們首先點進 parseInt() 方法中,

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s, 10);
}

可以看到,這個被我們調(diào)用的 parseInt() 方法返回了一個重載方法:

public static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) {
        throw new NumberFormatException("null");
    } else if (radix < 2) {
        throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
    } else if (radix > 36) {
        throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
    } else {
        boolean negative = false;
        int i = 0;
        int len = s.length();
        int limit = -2147483647;
        if (len <= 0) {
            throw NumberFormatException.forInputString(s);
        } else {
            char firstChar = s.charAt(0);
            if (firstChar < '0') {
                if (firstChar == '-') {
                    negative = true;
                    limit = -2147483648;
                } else if (firstChar != '+') {
                    throw NumberFormatException.forInputString(s);
                }
                if (len == 1) {
                    throw NumberFormatException.forInputString(s);
                }
                ++i;
            }
            int multmin = limit / radix;
            int result;
            int digit;
            for(result = 0; i < len; result -= digit) {
                digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0 || result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
            }
            return negative ? result : -result;
        }
    }
}

1、首先看到的是,該方法傳入了兩個參數(shù),parseInt(String s, int radix),這個可以根據(jù)被調(diào)用時傳入的參數(shù),return parseInt(s, 10);,盲猜一下,s 就是表示要轉(zhuǎn)換成數(shù)字型的字符串,而 radix 英文是基數(shù)的意思,這里應(yīng)該表示進制,即這個傳入的字符串是多少進制的,那到底是不是呢,我們接著往下看;

2、這里先是對字符串 s 是否為空,以及 radix 的大小進行一個判斷,不符合條件則拋出 NumberFormatException 異常,也就是數(shù)字格式化異常;

if (s == null){
    throw new NumberFormatException("null");
} else if (radix < 2) {
    throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
} else if (radix > 36) {
    throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
} else {

3、接著往下,再一次對長度進行一個校驗,

int len = s.length();
if (len <= 0) {
    throw NumberFormatException.forInputString(s);
} else {
	...
}

我在這里只想到了一個能讓它拋出異常的條件,

Integer.parseInt("");

運行結(jié)果:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)

4、接下來會檢測第一個字符是啥,如果是 -,則將 negative 設(shè)置成 true,表示這是個負數(shù),并且將邊界 limit 設(shè)置成最小邊界;

如果不是 +,則表示該字符既不是數(shù)字,不也是性質(zhì)符號,因此拋出 NumberFormatException 異常;

如果字符串 s 的長度只有1,則表明這是非數(shù)字,不符合要求,也拋出 NumberFormatException 異常;

++i 是因為如果第一位是符號的話,那么在后續(xù)的循環(huán)中追加數(shù)字則直接跳過首位;

 char firstChar = s.charAt(0);
 if (firstChar < '0') {
     if (firstChar == '-') {
         negative = true;
         limit = -2147483648;
     } else if (firstChar != '+') {
         throw NumberFormatException.forInputString(s);
     }
     if (len == 1) {
         throw NumberFormatException.forInputString(s);
     }
     ++i;
 }

5、根據(jù)進制來調(diào)整邊界,以防越界;

int multmin = limit / radix;

6、Character.digit() 用于將字符轉(zhuǎn)為為對應(yīng)進制的整數(shù),如果該字符不是進制內(nèi)的就返回-1,例如輸入的字符是9,但是進制是2,那么就不符合,則會返回-1;

然后就是進行計算;

int result;
int digit;
for(result = 0; i < len; result -= digit) {
    digit = Character.digit(s.charAt(i++), radix);
    if (digit < 0 || result < multmin) {
        throw NumberFormatException.forInputString(s);
    }
    result *= radix;
    if (result < limit + digit) {
        throw NumberFormatException.forInputString(s);
    }
}

7、最后判斷是否為負數(shù)完成轉(zhuǎn)換;

return negative ? result : -result;

valueOf

照例查看源碼:

public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return parseInt(s, radix);
}
public static Integer valueOf(String s) throws NumberFormatException {
    return parseInt(s, 10);
}
@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
    return i >= -128 && i <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[i + 128] : new Integer(i);
}

可以看出 valueOf(String s, int radix)valueOf(String s) 都是直接調(diào)用返回了 parseInt 方法,而 valueOf(int i) 則是一個 int 轉(zhuǎn)成 Integer 的自動裝箱;

接下來探究一下 IntegerCache ,可以看出這是 Integer 的成員內(nèi)部類,來看源碼:

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer[] cache;
    static Integer[] archivedCache;
    private IntegerCache() {
    }
    static {
        int h = 127;
        String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        int size;
        if (integerCacheHighPropValue != null) {
            try {
                size = Integer.parseInt(integerCacheHighPropValue);
                size = Math.max(size, 127);
                h = Math.min(size, 2147483518);
            } catch (NumberFormatException var6) {
            }
        }
        high = h;
        VM.initializeFromArchive(Integer.IntegerCache.class);
        size = high - -128 + 1;
        if (archivedCache == null || size > archivedCache.length) {
            Integer[] c = new Integer[size];
            int j = -128;
            for(int k = 0; k < c.length; ++k) {
                c[k] = new Integer(j++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
        assert high >= 127;
    }
}

整體就是初始化一個 IntegerCache.cache 數(shù)組,數(shù)組里面存儲-128到127之間的數(shù)字當做是緩存,源碼一開始是分析數(shù)組長度,然后給數(shù)組賦值;

總的來說,三個重構(gòu)的 valueOf() 方法還是大同小異的:

  • Integer valueOf(int i):返回一個表示指定的 int 值的 Integer 實例;
  • Integer valueOf(String s):返回保存指定的 String 的值的 Integer 對象;
  • Integer valueOf(String s, int radix):返回一個 Integer 對象,該對象中保存了用第二個參數(shù)提供的基數(shù)進行解析時從指定的 String 中提取的值;

以上就是Java 中 valueOf 和 parseInt 的區(qū)別探討解析的詳細內(nèi)容,更多關(guān)于Java valueOf與parseInt區(qū)別的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot Controller返回圖片的三種方式

    SpringBoot Controller返回圖片的三種方式

    在互聯(lián)網(wǎng)的世界里,圖片無處不在,它們是信息傳遞的重要媒介,也是視覺盛宴的一部分,而在Spring Boot項目中,如何優(yōu)雅地處理和返回圖片數(shù)據(jù),則成為了開發(fā)者們不得不面對的問題,今天,就讓我們一起來探索Spring Boot Controller的神奇轉(zhuǎn)換,需要的朋友可以參考下
    2024-07-07
  • 親測解決,nacos下線失敗問題

    親測解決,nacos下線失敗問題

    這篇文章主要介紹了親測解決,nacos下線失敗問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot打成jar包瘦身方法總結(jié)

    SpringBoot打成jar包瘦身方法總結(jié)

    springBoot打包的時候代碼和jar包打包在同一個jar包里面,會導(dǎo)致jar包非常龐大,下面這篇文章主要給大家介紹了關(guān)于SpringBoot打的jar包瘦身方法的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Java接口自動化測試框架設(shè)計之Get請求方法和測試詳解

    Java接口自動化測試框架設(shè)計之Get請求方法和測試詳解

    這篇文章主要介紹了Java接口自動化測試框架設(shè)計 Get請求方法和測試,框架設(shè)計我們只是介紹基本的組件,而且框架設(shè)計沒有想象那么難,一步一步跟著做就會了。這篇我們來演示,如果通過Java代碼來實現(xiàn)一個用純代碼實現(xiàn)Http中的Get請求過程,需要的朋友可以參考下
    2019-07-07
  • Java?Redis配置Redisson的方法詳解

    Java?Redis配置Redisson的方法詳解

    這篇文章主要為大家詳細介紹了Java?Redis配置Redisson的相關(guān)資料,文中的示例代碼講解詳細,對我們學習或工作有一定參考價值,感興趣的可以了解一下
    2022-08-08
  • Resty極簡restful框架快速接入Spring

    Resty極簡restful框架快速接入Spring

    這篇文章主要為大家介紹了Resty極簡的restful框架快速接入Spring詳細說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • 關(guān)于JWT之token令牌認證登錄

    關(guān)于JWT之token令牌認證登錄

    這篇文章主要介紹了關(guān)于JWT之token令牌認證登錄,使用JWT能夠保證Token的安全性,且能夠進行Token時效性的檢驗,使用JWT時,登錄成功后將用戶信息生成一串令牌字符串,需要的朋友可以參考下
    2023-05-05
  • java實現(xiàn)二分法查找出數(shù)組重復(fù)數(shù)字

    java實現(xiàn)二分法查找出數(shù)組重復(fù)數(shù)字

    這篇文章主要為大家詳細介紹了java實現(xiàn)二分法查找出數(shù)組重復(fù)數(shù)字,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Java中try-catch的使用及注意細節(jié)

    Java中try-catch的使用及注意細節(jié)

    現(xiàn)在有很多的語言都支持try-catch,比如常見的就是c++,java等,這篇文章主要給大家介紹了關(guān)于Java中try-catch的使用及注意細節(jié)的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • IntelliJ IDEA基于Maven構(gòu)建Java項目

    IntelliJ IDEA基于Maven構(gòu)建Java項目

    在 Java 開發(fā)中,使用 Maven 是一種廣泛采用的構(gòu)建工具,本文主要介紹了IntelliJ IDEA基于Maven構(gòu)建Java項目,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03

最新評論