long to double Java

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

The long and double, each takes 8 bytes of memory. The long holds a whole number and double holds a floating-point number. Assignment of long to double is done implicitly by JVM.

See the order of data types to know the rules of conversion.

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

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

Examples of implicit casting

int i1 = 10; // 4 bytes
double d1 = i1; // 4 bytes to 8 bytes

byte x = 10; // 1 byte
short y = x; // 1 byte to 2 bytes

byte x = 10; // 1 byte
int y = x; // 1 byte to 4 bytes

Following program on long to double explains implicit casting, Java style where a long is type casted to a double. It is a simple assignment.
public class Conversions
  public static void main(String args[])
    long l1 = 10;            
    double d1 = l1;  
    System.out.println("long value: " + l1);                // prints 10
    System.out.println("Converted double value: " + d1);    // prints 10.0

long to double
Output screenshot of long to double Java

double d1 = l1;

As long value l1 is assigned to double d1, the whole number is converted into floating-point value 10.0.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.