double Keyword in Java
The double keyword in Java is a primitive data type that represents a double-precision 64-bit IEEE 754 floating point. It is used to store decimal numbers with a higher degree of precision compared to the float data type.
Usage
The double data type is commonly used in situations that require a large range of values or a high degree of precision, such as scientific calculations, financial applications, and complex mathematical computations. The range for double values is approximately from 4.9e-324 to 1.7e+308.
Syntax
double variableName = value;
variableName: The name of the variable.value: The value to assign to the variable, which can include decimal points.
Examples
Example 1: Basic Usage
public class DoubleExample {
public static void main(String[] args) {
double pi = 3.14159;
double gravity = 9.81;
System.out.println("Value of pi: " + pi);
System.out.println("Value of gravity: " + gravity);
}
}
In this example, two double variables pi and gravity are declared and initialized with values 3.14159 and 9.81 respectively. The program then prints these values.
Example 2: Double Arithmetic
public class DoubleArithmetic {
public static void main(String[] args) {
double a = 5.5;
double b = 2.2;
double sum = a + b;
double product = a * b;
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
}
}
This example demonstrates arithmetic operations with double variables. The variables a and b are added and multiplied, and the results are stored in sum and product respectively.
Example 3: Double Array
public class DoubleArrayExample {
public static void main(String[] args) {
double[] temperatures = {36.6, 37.0, 36.8, 37.2};
for (double temp : temperatures) {
System.out.println("Temperature: " + temp);
}
}
}
This example shows the use of a double array. The array temperatures is initialized with four double values. A for-each loop iterates through the array and prints each temperature.
Tips and Best Practices
- Precision: Use
doublewhen you need more precision thanfloatcan provide. Thedoubledata type is more precise but also consumes more memory (64 bits). - Avoid Using for Exact Values: Avoid using
doublefor exact values like currency. For such cases, consider usingBigDecimal.BigDecimal value = new BigDecimal("123.45"); - Type Casting: Be cautious when converting between
doubleand other data types. Explicit type casting may be necessary.double a = 5.5; int b = (int) a; // Explicit type casting - Scientific Notation:
doublecan represent very large or very small numbers using scientific notation.double largeNumber = 1.23e10; // 1.23 * 10^10 double smallNumber = 1.23e-10; // 1.23 * 10^-10 - Use Double Class: For utility functions and methods involving doubles, consider using the
Doubleclass.Double doubleObject = Double.valueOf(a); // Using Double class
Common Constants
Positive Infinity
double positiveInfinity = Double.POSITIVE_INFINITY;
Negative Infinity
double negativeInfinity = Double.NEGATIVE_INFINITY;
NaN (Not a Number)
double notANumber = Double.NaN;
These constants can be used to handle special cases in floating-point arithmetic, such as results of undefined operations.