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


Java Short decode()用法及代碼示例

Short類decode()方法

  • decode() 方法可在java.lang包。
  • decode() 方法用於將給定的 String 值解碼為 Short 值。
  • decode() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
  • decode() 方法在將字符串解碼為 Short.NumberFormatException 時可能會拋出 NumberFormatException:在此異常中,如果給定的字符串參數沒有可解析的 short。

用法:

    public static Short decode(String str);

參數:

  • String str– 表示要解碼的字符串。

返回值:

這個方法的返回類型是Short,它返回持有由 String 類型參數表示的 short 值的 Short 。

例:

// Java program to demonstrate the example 
// of decode(String str) method of Short class

public class DecodeOfShortClass {
    public static void main(String[] args) {
        // Variables initialization
        short s = 100;
        String str = "20";

        // Short object initialized with "short" value
        Short value1 = new Short(s);

        // Display value1 result
        System.out.println("value1:" + value1);

        // It returns an Short object holding the short value 
        // denoted by the given String argument by calling 
        //value1.decode(str)

        Short result = value1.decode(str);

        // Display result
        System.out.println("value1.decode(str):" + result);
    }
}

輸出

value1:100
value1.decode(str):20


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Short class decode() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。