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 KeywordThe 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
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.
SyntaxFollowing 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 1Following 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.0Example 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.0Example 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4