byte to double Java

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

The byte takes 1 byte of memory and double takes 8 bytes of memory. Assignment 1 byte value to 4 bytes is done implicitly by the JVM. JVM does not take any process overhead for implicit casting. This is known as automatic promotion or widening conversion. It is so simple like pouring smaller bottle water into a bigger bottle.

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 double explains implicit casting. Observe, the Java syntax of doing so.

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


byte to double
Output screenshot of byte to double Java

1 byte value b1 is assigned to 8 bytes d1 and is type casted implicitly.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.