將字串解碼為整數的 Java 程式


Integer.decode() 方法將字串解碼為整數。它返回一個 Integer 物件,其中儲存由傳遞的字串值表示的 int 值。

− 它接受十進位制、十六進位制和八進位制數。

假設以下內容為我們的字串。

String val = "2";

以下是如何將字串解碼為整數。

Integer myInt = Integer.decode(val);

示例

 即時演示

public class Demo {
   public static void main(String []args) {
      String val = "2";
      Integer myInt = Integer.decode(val);
      System.out.println("Integer = " + myInt);
   }
}

輸出

Integer = 2

更新於: 26-6 月-2020

448 次瀏覽

開啟您的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.