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 Keyword (double Data Type)



double Keyword (double Data Type)

The double keyword is used to define a double-type variable that stores a double value (which is a floating-type value). The double is a primitive data type in Java.

Characteristics of double Keyword

The following are the characteristics of a double data type (or, double keyword):

  • 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

Double Type Variable

A double variable represents a reserved memory location 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.

Syntax

Following is the syntax to declare a double-type variable using the double keyword:

double variable_name = value;

Default Value

The default value of a double-type variable is 0.0d, here 'd' represents the double type.

Examples of double Keyword (double-type 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