Convert Byte to numeric primitive data types in Java


To convert Byte to numeric primitive data types, use the following methods −

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

Firstly, let us declare a Byte.

Byte byteVal = new Byte("35");

Now, let us see how to convert it to long type, for a simple example.

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

In the same way, you can convert it to other primitive data types as shown in the complete example below −

Example

 Live Demo

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);
   }
}

Output

35
35
35
35
35.0
35.0

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements