double-java

Double Java

Java double keyword

The Java double keyword is a primitive data type. It is a double-precision 64-bit IEEE 754 floating point. It is used to declare the variables and methods. It generally represents the decimal numbers.

Points to remember

  • The double covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
  • Its default value is 0.0d.
  • Its default size is 8 byte.
  • It is the default type for decimal numbers.
  • It is not a good approach to use double for precise values, such as currency.

Examples of Java double keyword

Example 1

Let’s see a simple example to display double type variable.

  1. public class DoubleExample1 {
  2.     public static void main(String[] args) {
  3.         double num=5.5;
  4.         System.out.println(“num: “+num);
  5.     }
  6. }

Output:

num: 5.5

Example 2

In this example, we provide integer value to double variable. Here, compiler implicitly typecast integer to double and display the corresponding value in decimal form.

  1. public class DoubleExample2 {
  2.     public static void main(String[] args) {
  3.         double num1=5;
  4.         double num2=10;
  5.         System.out.println(“num1: “+num1);
  6.         System.out.println(“num2: “+num2);
  7.     }
  8. }

Output:

num1: 5.0
num2: 10.0

Example 3

Let’s see an example to test the bigger decimal value.

  1. public class DoubleExample3 {
  2.     public static void main(String[] args) {
  3.         double num1=581216732.323433;
  4.         double num2=7.83684987683688;
  5.         System.out.println(“num1: “+num1);
  6.         System.out.println(“num2: “+num2);
  7.     }
  8. }

Output:

num1: 5.81216732323433E8
num2: 7.83684987683688

Example 4

In this example, we provide float value to decimal variable.

  1. public class DoubleExample4 {
  2.     public static void main(String[] args) {
  3.         double num1=56.34f;
  4.       double num2=34f;
  5.         System.out.println(“num1: “+num1);
  6.         System.out.println(“num2: “+num2);
  7.     }
  8. }

Output:

num1: 56.34000015258789
num2: 34.0

Example 5

In this example, we provide the maximum range of decimal value.

  1. public class DoubleExample5 {
  2.     public static void main(String[] args) {
  3.         double num1=4.94065645841246544e-324d;
  4.         double num2=1.79769313486231570e+308d;
  5.         System.out.println(“num1: “+num1);
  6.         System.out.println(“num2: “+num2);
  7.     }
  8. }

Output:

num1: 4.9E-324
num2: 1.7976931348623157E308

Example 6

In this example, we provide the value in scientific notation

  1. public class DoubleExample6 {
  2.     public static void main(String[] args) {
  3.         double num1=1873.2;
  4.         //providing same value in scientific notation
  5.         double num2=1.8732e3;
  6.         System.out.println(“num1: “+num1);
  7.         System.out.println(“num2: “+num2);
  8.     }
  9. }

Output:

num1: 1873.2
num2: 1873.2

Example 7

Let’s see an example to create a method of the double return type.

  1. public class DoubleExample7 {
  2.     public double display(double weight)
  3.     {
  4.         return weight;
  5.     }
  6.     public static void main(String[] args) {
  7.         DoubleExample7 d=new DoubleExample7();
  8.         System.out.println(d.display(62.5));
  9.     }
  10. }

Output:

62.5