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


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

Short類intValue()方法

  • intValue() 方法可在java.lang包。
  • intValue() 方法用於返回由轉換為 int 類型(通過強製轉換)的 Short 對象表示的值。
  • intValue() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • intValue() 方法從 short 到 int 的轉換時不拋出異常。

用法:

    public int intValue();

參數:

  • 它不接受任何參數。

返回值:

這個方法的返回類型是int,它返回一個由這個 Short 對象表示的轉換後的(從 short 類型到 int 類型)值。

例:

// Java program to demonstrate the example 
// of intValue() method of Short class

public class IntValueOfShortClass {
    public static void main(String[] args) {
        // Variables initialization
        short value1 = 100;
        short value2 = 200;

        // It returns short value denoted by this Short ob1 object
        // and converted to a int by calling ob1.intValue()
        Short ob1 = new Short(value1);

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

        // It returns short value denoted by this Short ob2 object
        // and converted to a int by calling ob2.intValue()
        Short ob2 = new Short(value2);

        // Display ob2 result
        System.out.println("ob2.intValue():" + ob2.intValue());
    }
}

輸出

ob1.intValue():100
ob2.intValue():200


相關用法


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