byte to short Java

After knowing the Java rules of Data Type Casting (Type Conversion), let us cast byte to short. The byte takes 1 byte of memory and short takes two bytes of memory. Assignment 1 byte value to 2 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 example on byte to short illustrates the syntax of automatic promotion of widening conversion of Java.
public class Conversions
{
  public static void main(String args[])
  {
    byte b1 = 10;                // 1 byte  
    short s1 = b1;               //  one byte to 2 bytes
    
    System.out.println("byte value: " + b1);                // prints 10
    System.out.println("Converted short value: " + s1);     // prints 10
  }
}


byte to short
Output screenshot of byte to short Java

1 byte value b1 is assigned to 2 bytes s1 and is type casted implicitly.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.