Java Convert byte to long

Tags: byte long

In this Java core tutorial we learn how to convert a byte value into long value with different solutions in Java programming language.

Table of contents

  1. Assign byte variable to long variable in Java
  2. Using Long.valueOf() method
  3. Using Byte.longValue() method

Assign byte variable to long variable in Java

In this first solution, to convert byte to long we just simply assign the byte variable directly to a long variable as the following Java code.

ConvertByteToLongExample1.java

public class ConvertByteToLongExample1 {
    public static void main(String... args) {
        byte byteValue = 99;

        // Assign byte variable to long variable
        long longValue = byteValue;

        System.out.println("byte value: " + byteValue);
        System.out.println("long value: " + longValue);
    }
}
The output as below.
byte value: 99
long value: 99

Using Long.valueOf() method

In this second solution we use the Long.valueOf(long l) static method to convert a byte value to long as the Java code below.

ConvertByteToLongExample2.java

public class ConvertByteToLongExample2 {
    public static void main(String... args) {
        byte byteValue = 67;

        // Convert byte to long
        long longValue = Long.valueOf(byteValue);

        System.out.println("byte value: " + byteValue);
        System.out.println("long value: " + longValue);
    }
}
The output as below.
byte value: 67
long value: 67

Using Byte.longValue() method

In this third solution, with a given Byte object we can use the Byte.longValue() method to convert it to a long value as the example Java code below.

ConvertByteToLongExample3.java

public class ConvertByteToLongExample3 {
    public static void main(String... args) {
        Byte byteValue = 11;

        // Convert byte to long
        long longValue = byteValue.longValue();

        System.out.println("byte value: " + byteValue);
        System.out.println("long value: " + longValue);
    }
}
The output as below.
byte value: 11
long value: 11

Happy Coding 😊

Java Convert long to char

Java Convert char to long

Java Convert long to byte