當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.Integer.valueOf()用法及代碼示例


描述

這個java.lang.Integer.valueOf(String s) 方法返回一個包含指定字符串值的 Integer 對象s

聲明

以下是聲明java.lang.Integer.valueOf()方法

public static Integer valueOf(String s) throws NumberFormatException

參數

s- 這是要解析的字符串。

返回值

此方法返回一個 Integer 對象,其中包含由字符串參數表示的值。

異常

NumberFormatException- 如果字符串不能解析為整數。

示例

下麵的例子展示了 java.lang.Integer.valueOf() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class IntegerDemo {

   public static void main(String[] args) {

      Integer i = new Integer(30);
   
      String str = "5";
      // returns a Integer instance representing the specified string
      System.out.println("Value = " + i.valueOf(str));
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

Value = 5

相關用法


注:本文由純淨天空篩選整理自 Java.lang.Integer.valueOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。