溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些

發(fā)布時間:2021-09-09 10:10:37 來源:億速云 閱讀:132 作者:小新 欄目:編程語言

小編給大家分享一下Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

先來看段代碼

public class IntegerDemo {
  public static void main(String[] args) {
    String num = null;
    System.out.println( Integer.parseInt(num));// Exception java.lang.NumberFormatException
    System.out.println( Integer.valueOf(num));// Exception java.lang.NumberFormatException
    System.out.println( String.valueOf(num)); //輸出null
    num = "";
    System.out.println( Integer.parseInt(num)); // Exception java.lang.NumberFormatException
    System.out.println( Integer.valueOf(num)); // Exception java.lang.NumberFormatException
    System.out.println( String.valueOf(num));//空串,啥也不輸出
  }
}

先看一下 String.valueOf() 里面是怎么寫的

Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些

String.valueOf() 在遇到 null 和 空串的情況下 ,都能正常輸出,所以不拋錯

再來看下 包裝類型 Integer里面又是如何處理的

Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些

Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些

這兩個方法里面都需要先 parseInt( s,10),就是將字符串s先轉(zhuǎn)成  十進制的 int基本類型,,但是 valueOf()會根據(jù)int范圍從[-127,127]的內(nèi)部緩存中去取(用到設(shè)計模式中的 享元模式)

一起來看下 parseInt(s, 10),,在方法里面會判斷字符串是否是合法的數(shù)字,會去校驗null, 空串等其他格式,所以會拋錯

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

看完了這篇文章,相信你對“Java中Integer.valueOf,parsetInt() String.valueOf的區(qū)別有哪些”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI