Java Tutorial Java Advanced Java References

Java double Keyword



The Java double keyword is a primitive data types. The double data type is a double-precision 64-bit IEEE 754 floating point. Its default value is 0.0d and default size is 64 bits (8 byte). The range of a double value is 4.9406564584124654 x 10-324 to 1.7976931348623157 x 10308.

Example: Create a double value

In the example below, variable x and y are created to store double values. Please note that, value with a "d" is used to represent the double value.

public class MyClass {
  public static void main(String[] args) {   
    //creating double values
    double x = 10.5d;
    double y = 10.5;

    //printing variables
    System.out.println("x = " + x);
    System.out.println("y = " + y);    
  }
}

The output of the above code will be:

x = 10.5
y = 10.5

Example: Widening Casting (smaller to larger type)

A double value can be created by assigning a byte, a short, an int, a long or a float data to a double variable. The compiler implicitly typecast these data types to double as shown in the output.

public class MyClass {
  public static void main(String[] args) {      
    byte num_byte = 10;

    //Widening Casting
    short num_short = num_byte;
    int num_int = num_short;
    long num_long = num_int;
    float num_float = num_long;
    double num_double = num_float;

    //printing variables
    System.out.println("num_byte = " + num_byte); 
    System.out.println("num_short = " + num_short);
    System.out.println("num_int = " + num_int);
    System.out.println("num_long = " + num_long);
    System.out.println("num_float = " + num_float); 
    System.out.println("num_double = " + num_double); 
  }
}

The output of the above code will be:

num_byte = 10
num_short = 10
num_int = 10
num_long = 10
num_float = 10.0
num_double = 10.0

❮ Java Keywords




5