Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the Double
class is a wrapper class for the primitive data type double
. It is part of the java.lang
package and provides an object-oriented way to work with double values, including methods for converting, parsing, and comparing double values. In this tutorial, we'll explore some common operations using the Double
class.
To create a Double
object, you can use the constructor or the valueOf()
method:
Double d1 = new Double(3.14); Double d2 = Double.valueOf(3.14);
Since Java 9, the constructor is deprecated, so it is recommended to use the valueOf()
method.
To convert a double
value to a String
, you can use the toString()
method or the Double.toString()
static method:
Double d1 = 3.14; String str1 = d1.toString(); String str2 = Double.toString(3.14);
To parse a String
to a double
, you can use the parseDouble()
static method:
String str = "3.14"; double d = Double.parseDouble(str);
To compare Double
values, you can use the compareTo()
method or the Double.compare()
static method:
Double d1 = 3.14; Double d2 = 2.71; int result1 = d1.compareTo(d2); // Returns 1 if d1 > d2, 0 if d1 == d2, -1 if d1 < d2 int result2 = Double.compare(3.14, 2.71); // Returns 1 if 3.14 > 2.71, 0 if 3.14 == 2.71, -1 if 3.14 < 2.71
To convert between double
and Double
, you can use auto-boxing and auto-unboxing:
Double d1 = 3.14; // Auto-boxing (primitive to wrapper) double d2 = d1; // Auto-unboxing (wrapper to primitive)
The Double
class provides some useful constants:
Double.MAX_VALUE
: The largest positive finite value that a double
can represent.Double.MIN_VALUE
: The smallest positive nonzero value that a double
can represent.Double.NaN
: Represents "Not-a-Number", a special value that is not equal to any numeric value.Double.POSITIVE_INFINITY
: Represents positive infinity.Double.NEGATIVE_INFINITY
: Represents negative infinity.Example:
System.out.println("Max value: " + Double.MAX_VALUE); System.out.println("Min value: " + Double.MIN_VALUE); System.out.println("NaN: " + Double.NaN); System.out.println("Positive infinity: " + Double.POSITIVE_INFINITY); System.out.println("Negative infinity: " + Double.NEGATIVE_INFINITY);
In summary, the Double
class in Java is a wrapper class for the primitive data type double
. It provides an object-oriented way to work with double values and offers methods for converting, parsing, and comparing double values. You can also use auto-boxing and auto-unboxing to convert between double
and Double
. The Double
class also provides some useful constants related to double values.
Converting string to double in Java Double class
The Double
class provides methods for converting strings to double
values. Here's an example:
String str = "3.14"; double doubleValue = Double.parseDouble(str);
Precision and rounding with Java Double
Precision issues may arise due to the binary representation of floating-point numbers. Rounding can be applied using methods like Math.round()
.
double originalValue = 3.145678; double roundedValue = Math.round(originalValue * 100.0) / 100.0;
Double.valueOf()
vs. new Double()
in Java
Double.valueOf()
returns a Double
object representing the specified double
value, while new Double()
creates a new Double
object. Prefer valueOf()
for better performance.
double value = 3.14; Double double1 = Double.valueOf(value); Double double2 = new Double(value);
Handling NaN and Infinity in Java Double
Double
class provides constants for NaN
and POSITIVE_INFINITY
/NEGATIVE_INFINITY
. You can check for these special values using Double.isNaN()
and Double.isInfinite()
.
double result = 1.0 / 0.0; // Infinity System.out.println(Double.isInfinite(result)); // true
Formatting double values in Java
Formatting double
values for display can be done using String.format()
or DecimalFormat
.
double value = 12345.6789; String formattedValue = String.format("%.2f", value);
Parsing and formatting doubles with DecimalFormat
in Java
DecimalFormat
provides more control over formatting double
values, allowing you to specify patterns for decimal places, commas, etc.
import java.text.DecimalFormat; double value = 12345.6789; DecimalFormat df = new DecimalFormat("#,##0.00"); String formattedValue = df.format(value);
Comparing double values in Java with Double.compare()
Comparing double
values for equality can be tricky due to precision issues. Double.compare()
provides a reliable way to compare double
values.
double value1 = 0.1 + 0.1 + 0.1; double value2 = 0.3; int result = Double.compare(value1, value2); if (result == 0) { System.out.println("Values are equal"); } else { System.out.println("Values are not equal"); }
Java Double to int conversion example
Converting double
to int
involves truncating the decimal part. Be cautious about potential loss of precision.
double doubleValue = 3.14; int intValue = (int) doubleValue;