Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - double Datatype



Java double datatype is one of the eight primitive datatype supported by Java. It provides means to create double type variables which can accept a double value. Following are the characteristics of a double data type.

  • double data type is a double-precision 64-bit IEEE 754 floating point

  • This data type is generally used as the default data type for decimal values, generally the default choice

  • Double data type should never be used for precise values such as currency

  • Default value is 0.0d

  • Example: double d1 = 123.4

A double variables represents a reserved memory locations to store double values. This means that when you create a variable you reserve some space in the memory.

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store double values in double variables.

Example 1

Following example shows the usage of double primitive data type we've discussed above. We've created a double variable as doubleValue and assigned it a double value. Then this variable is printed.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      double doubleValue = 2.0;
      System.out.println("double: " + doubleValue);	  
   }
}

Output

double: 2.0

Example 2

Following example shows the usage of double primitive data type in an expression. We've created two double variables and assigned them double values. Then we're creating a new double variable doubleResult to assign it the sum of double variables. Finally result is printed.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      double doubleValue1 = 2.0;
      double doubleValue2 = 4.0;
      double doubleResult = doubleValue1 + doubleValue2;

      System.out.println("double: " + doubleResult);
   }
}

Output

double: 6.0

Example 3

Following example shows the usage of double variable with an invalid value. We've created a double variable as doubleValue and assigned it an out of range value.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      double doubleValue = 2 * Double.MAX_VALUE;
      System.out.println("double: " + doubleValue);
   }
}

Output

double: Infinity
Advertisements