Convert Byte to numeric primitive data types in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 12-Nov-2018 12:25:56
Advertisements