在 Java 中將 Byte 轉換為數字基本資料型別


要將 Byte 轉換為數字基本資料型別,請使用以下方法 -

byteValue()
shortValue()
intValue()
longValue()
floatValue()

首先,讓我們宣告一個 Byte。

Byte byteVal = new Byte("35");

現在,讓我們看看如何將其轉換為長型別,這是一個簡單的示例。

long longVal = byteVal.longValue();
System.out.println(longVal);

同樣,你可以將其轉換為其他基本資料型別,如下面完整示例所示 -

示例

 線上演示

public class Demo {
   public static void main(String args[]) {
      // byte
      Byte byteVal = new Byte("35");
      byte b = byteVal.byteValue();
      System.out.println(b);
      short shortVal = byteVal.shortValue();
      System.out.println(shortVal);
      int intVal = byteVal.intValue();
      System.out.println(intVal);
      long longVal = byteVal.longValue();
      System.out.println(longVal);
      float floatVal = byteVal.floatValue();
      System.out.println(floatVal);
      double doubleVal = byteVal.doubleValue();
      System.out.println(doubleVal);
   }
}

輸出

35
35
35
35
35.0
35.0

更新日期:26-6-2020

342 瀏覽量

開啟你的 職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.