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

Java如何將字符串String轉(zhuǎn)換為整型Int

 更新時間:2022年08月10日 14:57:47   作者:碼說TM  
這篇文章主要介紹了Java如何將字符串String轉(zhuǎn)換為整型Int,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下

用法

在java中經(jīng)常會遇到需要對數(shù)據(jù)進行類型轉(zhuǎn)換的場景,String類型的數(shù)據(jù)轉(zhuǎn)為Int類型屬于比較常見的場景,主要有兩種轉(zhuǎn)換方法:

  • 1. 使用Integer.parseInt(String)方法
  • 2. 使用Integer.valueOf(String)方法

具體demo如下:

public void convert() {
    // 1.使用Integer.parseInt(String)
    String str1 = "31";
    Integer num1 = Integer.parseInt(str1);
    System.out.print("字符串31轉(zhuǎn)換為數(shù)字:");
    System.out.println(num1);

    // 2.使用Integer.valueOf(String)
    String str2 = "32";
    Integer num2 = Integer.valueOf(str2);
    System.out.print("字符串32轉(zhuǎn)換為數(shù)字:");
    System.out.println(num2);
}

執(zhí)行結(jié)果:

根據(jù)執(zhí)行結(jié)果可見,兩種方式都能完成字符串到整型的轉(zhuǎn)換。

注意點

但需要注意的是,使用這兩種方法都有一個前提,那就是待轉(zhuǎn)換字符串的內(nèi)容必須為純數(shù)字。 

不難發(fā)現(xiàn)上面demo中的待轉(zhuǎn)換字符串都是"31"、"32"這種由純數(shù)字組成的字符串,如果待轉(zhuǎn)字符串中出現(xiàn)了除數(shù)字以外的其他字符,則程序會拋出異常。

如下demo所示,在字符串中加入小寫英文字母,并用try-catch語句包裹代碼段以捕捉會出現(xiàn)的異常。(因為我們已經(jīng)知道,帶字母的字符串轉(zhuǎn)換為整型會出現(xiàn)數(shù)字格式轉(zhuǎn)換的異常,所以選擇catch NumberFormatException)

public void convert() {
    // 1.Integer.parseInt(String)
    try {
        String str1 = "31a";
        Integer num1 = Integer.parseInt(str1);
        System.out.print("字符串31a轉(zhuǎn)換為數(shù)字:");
        System.out.println(num1);
    } catch (NumberFormatException e) {
        System.out.println("Integer.parseInt(String)方法執(zhí)行異常");
        e.printStackTrace();
    }
 
    // 1.Integer.valueOf(String)
    try {
        String str2 = "32b";
        Integer num2 = Integer.valueOf(str2);
        System.out.print("字符串32b轉(zhuǎn)換為數(shù)字:");
        System.out.println(num2);
    } catch (NumberFormatException e) {
        System.out.println("Integer.valueOf(String)方法執(zhí)行異常");
        e.printStackTrace();
    }
}

從執(zhí)行結(jié)果可見,這段代碼分別在Integer.parseInt(String)方法和Integer.valueOf(String)位置觸發(fā)了NumberFormatException,其原因都是被轉(zhuǎn)換的字符串中存在英文字母,無法轉(zhuǎn)換成整型。

性能比較

我們可以通過使用System.nanoTime()來查看兩種方法執(zhí)行的時間差

public static void convert() {
    // 1.Integer.parseInt(String)
    String str1 = "321";
    long before1 = System.nanoTime();
    Integer.parseInt(str1);
    long interval1 = System.nanoTime() - before1;
    System.out.print("Integer.parseInt(String)的執(zhí)行時長(納秒):");
    System.out.println(interval1);

    // 1.Integer.valueOf(String)
    String str2 = "332";
    long before2 = System.nanoTime();
    Integer.valueOf(str2);
    long interval2 = System.nanoTime() - before2;
    System.out.print("Integer.valueOf(String)的執(zhí)行時長(納秒):");
    System.out.println(interval2);
}

其中,interval1和interval2的值分別指兩個方法的執(zhí)行前后系統(tǒng)時間之差,單位是納秒,執(zhí)行多次后均可發(fā)現(xiàn),Integer.valueOf(String)方法的執(zhí)行時長要小于Integer.parseInt(String)方法,性能更優(yōu) 

到此這篇關(guān)于Java如何將字符串String轉(zhuǎn)換為整型Int的文章就介紹到這了,更多相關(guān)Java String轉(zhuǎn)換為Int內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論