Double is a wrapper class,

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

The double data type,

The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

Check each datatype with their ranges : Java's Primitive Data Types.


Important Note : If you'r thinking to use double for precise values, you need to re-think before using it. Java Traps: double

Answer from Saurabh Gokhale on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Double.html
Double (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, ...
🌐
DataCamp
datacamp.com › doc › java › double
double Keyword in Java: Usage & Examples
Avoid Using for Exact Values: Avoid using double for exact values like currency. For such cases, consider using BigDecimal. ... Type Casting: Be cautious when converting between double and other data types. Explicit type casting may be necessary. double a = 5.5; int b = (int) a; // Explicit ...
Discussions

When should I use double and when should I use float for Java?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
65
59
January 6, 2024
[JAVA] Cannot convert from double to decimal format.

DecimalFormat is not a numerical type. It does not do calculations or hold numbers. It is a type designed to help PRINT OUT a numerical type.

    double taxTotal;
    DecimalFormat formatter = new DecimalFormat ("$###,###.##");
    // blah blah

   string formattedValue = formatter.format(taxTotal)
   System.out.print("Your 2012 tax is " + formattedValue );

Note: You shouldn't use floating point types (i.e. float or duuble) to store money

  1. http://www.javapractices.com/topic/TopicAction.do?Id=13

  2. http://stackoverflow.com/questions/285680/representing-monetary-values-in-java

More on reddit.com
🌐 r/learnprogramming
13
2
February 20, 2014
Can someone help me understand why these variables take on these values?
In each question you are dealing with the division of 2 int values. Therefore each result is going to be an int. Even in the case where the result is cast to a double, the answer evaluated to an in type (2) then was cast to an double which gives you 2.0. And yes if you cast 2.9999999 to an int you will get 2. Java does not assume that when you cast you want to round the number. You simply lose the precision. Dealing with integer division is like doing long division. You get a whole number as a result while the remainder would be the value returned if you used the modulo (%) operator. Someone can likely give you more detailed answers or answers as to why but I'm on a bus on my mobile. More on reddit.com
🌐 r/javahelp
7
1
September 19, 2014
Trying to generate a random double on the range (0.0,1.0]
So Math.random() gives you 0.0 (inclusive) to 1.0 (exclusive), while you want 0.0 (exclusive) to 1.0 (inclusive). Can you just do 1.0-Math.random()? More on reddit.com
🌐 r/learnjava
3
1
April 20, 2017
Top answer
1 of 3
38

Double is a wrapper class,

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

The double data type,

The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

Check each datatype with their ranges : Java's Primitive Data Types.


Important Note : If you'r thinking to use double for precise values, you need to re-think before using it. Java Traps: double

2 of 3
29

In a comment on @paxdiablo's answer, you asked:

"So basically, is it better to use Double than Float?"

That is a complicated question. I will deal with it in two parts


Deciding between double versus float

On the one hand, a double occupies 8 bytes versus 4 bytes for a float. If you have many of them, this may be significant, though it may also have no impact. (Consider the case where the values are in fields or local variables on a 64bit machine, and the JVM aligns them on 64 bit boundaries.) Additionally, floating point arithmetic with double values is typically slower than with float values ... though once again this is hardware dependent.

On the other hand, a double can represent larger (and smaller) numbers than a float and can represent them with more than twice the precision. For the details, refer to Wikipedia.

The tricky question is knowing whether you actually need the extra range and precision of a double. In some cases it is obvious that you need it. In others it is not so obvious. For instance if you are doing calculations such as inverting a matrix or calculating a standard deviation, the extra precision may be critical. On the other hand, in some cases not even double is going to give you enough precision. (And beware of the trap of expecting float and double to give you an exact representation. They won't and they can't!)

There is a branch of mathematics called Numerical Analysis that deals with the effects of rounding error, etc in practical numerical calculations. It used to be a standard part of computer science courses ... back in the 1970's.


Deciding between Double versus Float

For the Double versus Float case, the issues of precision and range are the same as for double versus float, but the relative performance measures will be slightly different.

  • A Double (on a 32 bit machine) typically takes 16 bytes + 4 bytes for the reference, compared with 12 + 4 bytes for a Float. Compare this to 8 bytes versus 4 bytes for the double versus float case. So the ratio is 5 to 4 versus 2 to 1.

  • Arithmetic involving Double and Float typically involves dereferencing the pointer and creating a new object to hold the result (depending on the circumstances). These extra overheads also affect the ratios in favor of the Double case.


Correctness

Having said all that, the most important thing is correctness, and this typically means getting the most accurate answer. And even if accuracy is not critical, it is usually not wrong to be "too accurate". So, the simple "rule of thumb" is to use double in preference to float, UNLESS there is an overriding performance requirement, AND you have solid evidence that using float will make a difference with respect to that requirement.

🌐
Software Testing Help
softwaretestinghelp.com › home › java › java double – tutorial with programming examples
Java Double - Tutorial With Programming Examples
April 1, 2025 - Answer: The MathContext is a class in Java that specifies the rounding-off number mode and precision. It provides immutable objects and is also responsible for imposing certain rules for the operators that are implemented by Big Decimal class. ... In the below example, we have initialized a double variable and set different rules of rounding the digits. This works in accordance with the output specifier that we have passed.
🌐
CodeGym
codegym.cc › java blog › java numbers › java double keyword
Java double keyword
February 19, 2025 - Java provides primitive data types like double for efficiency and simplicity. They aren’t objects, so they don’t come with object-oriented features. On the flip side, wrapper classes like Double wrap those primitive values in an object. That means you can store them in data structures that only accept objects, such as Collections. For example, a List<Double> can hold many doubles, but it can’t hold the primitive double.
🌐
W3Schools
w3schools.com › java › ref_keyword_double.asp
Java double Keyword
Java Examples Java Videos Java ... Study Plan Java Interview Q&A ... The double keyword is a data type that can store fractional numbers from 1.7e−308 to 1.7e+308....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-double-class-java
Java.Lang.Double Class in Java - GeeksforGeeks
July 23, 2025 - B. Double(String s): Creates a Double-object initialized with the parsed double value provided by string representation where it takes a string representation of the byte value as a parameter.
Find elsewhere
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Keywords › double
Java Programming/Keywords/double - Wikibooks, open books for an open world
The java.lang.Double class is the nominal wrapper class when you need to store a double value but an object reference is required. Syntax: double <variable-name> = <float-value>; For example: See also: Java Programming/Primitive Types · Retrieved from "https://en.wikibooks.org/w/index.php...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.double
Double Class (Java.Lang) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. The Double class wraps a value of the primitive type double in an object. [Android.Runtime.Register("java/lang/Double", DoNotGenerateAcw=true)] public sealed class Double : Java.Lang.Number, IConvertible, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IComparable
🌐
iO Flood
ioflood.com › blog › java-double
Java Double Keyword: Your Guide to Decimal Precision
February 26, 2024 - The double keyword is used to identify a data type in Java that is used to store decimal numbers, instantiated with the syntax double var = 3.14;. It provides a way to work with numbers that have a decimal point and need a high degree of precision.
🌐
Home and Learn
homeandlearn.co.uk › java › double_variables.html
java for complete beginners - double variables
The double variable is also used to hold floating point values. A floating point value is one like 8.7, 12.5, 10.1. In other words, it has a "point something" at the end. If you try to store a floating point value in an int variable, NetBeans will underline the faulty code.
🌐
Quora
quora.com › What-is-double-in-Java
What is (double) in Java? - Quora
Though both Java float vs Double is approximate types, if you need more precise and accurate result then use double.Use float if you have memory constraint because it takes almost half as much space as double. ... A float is a variable data type used to store decimal numbers with a decimal point in it or more technically, floating point numbers, which is what is it called in programming. For example...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Float-vs-Double-Whats-the-difference
Java double vs float: What's the difference?
The lower range of a float in Java is 1.4E-45. ... Specify the type as double. Provide a variable name. Assign a value within the range of a double. ... Specify the type as float. Provide a variable name. Assign a value within the range of a float. ... When Java encounters a decimal in a program, or a calculation generates a floating point number, Java treats the number as a double. For example, both of the following lines of code fail to compile because Java assumes that any decimal is a double.
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › lang › Double.html
Double (Java SE 23 & JDK 23)
October 17, 2024 - For example, in base 10, 1/3 is a repeating fraction (0.33333....); but in base 3, 1/3 is exactly 0.1(3), that is 1 × 3-1. Similarly, in base 10, 1/10 is exactly representable as 0.1 (1 × 10-1), but in base 2, it is a repeating fraction (0.0001100110011...(2)). Values of the float type have 24 bits of precision and values of the double type have 53 bits of precision. Therefore, since 0.1 is a repeating fraction in base 2 with a four-bit repeat, 0.1f != 0.1d.
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Double.html
Double (Java SE 24 & JDK 24)
July 15, 2025 - For example, in base 10, 1/3 is a repeating fraction (0.33333....); but in base 3, 1/3 is exactly 0.1(3), that is 1 × 3-1. Similarly, in base 10, 1/10 is exactly representable as 0.1 (1 × 10-1), but in base 2, it is a repeating fraction (0.0001100110011...(2)). Values of the float type have 24 bits of precision and values of the double type have 53 bits of precision. Therefore, since 0.1 is a repeating fraction in base 2 with a four-bit repeat, 0.1f != 0.1d.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Double.html
Double (Java SE 21 & JDK 21)
January 20, 2026 - In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double. This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
🌐
Sololearn
sololearn.com › en › Discuss › 1812370 › what-does-double-do-in-java
What does "double" do in java? | Sololearn: Learn to code for FREE!
May 22, 2019 - *Program examples using "double": ... 5.0 ---------------------------------------------- ... Double is a primitive data type for decimal numbers like 2.5; 1.333; 4.0... https://www.baeldung.com/java-primitive...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, ...
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Double.html
Double (Java Platform SE 6)
P where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of the of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_double.htm
Java - Double class
static Class<Double> TYPE − This is the class instance representing the primitive type double · This class inherits methods from the following classes − ... The following example shows the usage of Double class to get double from a string.