int to double Java


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

The int takes 4 bytes of memory and double takes 8 bytes of memory. Assignment of int to double is done implicitly by JVM. It is known as automatic promotion or widening 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; // byte is type converted to short

Following program on int to double explains implicit casting, Java style where int is assigned to a double.

public class Conversions
{
  public static void main(String args[])
  {
    int i1 = 10;                     // 4 bytes
    double d1 = i1;                  // int is type converted to double

    System.out.println("int value: " + i1);               // prints 10
    System.out.println("Converted double value: " + d1);  // prints 10.0
  }
}


ss
Output screenshot of int to double Java

double d1 = i1;

As int value i1 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.