byte to long Java


After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast byte to long as an example.

The byte takes 1 byte of memory and long takes 8 bytes of memory. Assignment 1 byte value to 8 bytes is done implicitly by the JVM.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse requires explicit casting.

Examples:

       short x = 10;
       int y = x;

       byte x = 10;
       short y = x;

Following program on byte to long Java explains implicit casting. Observe, the Java syntax.

public class Conversions
{
  public static void main(String args[])
  {
    byte b1 = 10;                 // 1 byte  
    long l1 = b1;                 //  one byte to 8 bytes, assigned implicitly
    
    System.out.println("byte value: " + b1);               //  prints 10
    System.out.println("Converted long value: " + l1);     //  prints 10
  }
}


byte to long
Output screenshot of byte to long Java

1 byte value b1 is assigned to 8 bytes l1 and is type casted implicitly. This is known as widening conversion where automatic promotion takes place. This conversion does not need any extra overhead to JVM. It is so simple where a smaller bottle water is poured in a bigger bottle.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.