As was already mentioned, the term is autoboxing. The object wrappers for the primitive types will automatically convert.

As to your second part,

Double a = 2;

Doesn't work since 2 is not a double and the auto boxing only works between the same types. In this case 2 is an int.

But if you cast it.

Double a = (double)2;

works just fine.

double a = 2;

works because an int can be automatically converted to a double. But going the other way doesn't work.

int a = 2.2; // not permitted.

Check out the Section on conversions. In the Java Language Specification. Warning that it can sometimes be difficult to read.

Amended Answer.

In java you can cast up or down or have narrowing or widening casts (going from a 32 bit to 16 bit) value is narrowing. But I tend to think about it is losing vs not losing something. In most cases if you have the potential to lose part of value in assignment, you need to cast, otherwise you don't (See exceptions at end). Here are some examples.

long a = 2; // 2 is an integer but going to a long doesn't `lose` precision.
int b = 2L; // here, 2 is a long and the assignment is not permitted.  Even 
            // though a long 2 will fit inside an int, the cast is still 
            // required.
int b = (int)2L;  // Fine, but clearly a contrived case

Same for floating point.

float a = 2.2f; // fine
double b = a;   // no problem, not precision lost
float c = b;    // can't do it, as it requires a cast.

double c = 2.2f; // a float to a double, again a not problem.
float d = 2.2;  // 2.2 is a double by default so requires a cast or the float designator.
float d = (float)2.2;

Exceptions

No cast is required when converting from int to float or long to double. However, precision can still be lost since the floats only have 24 bits of precision and doubles only have 53 bits of precision.

To see this for ints you can run the following:

        for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE-100; i--) {
            float s = i;
            int t = (int)s; // normal cast required
            if (i != t) {
                System.out.println (i + " " + t);
            }
        }
Answer from WJS 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.
Top answer
1 of 2
2

As was already mentioned, the term is autoboxing. The object wrappers for the primitive types will automatically convert.

As to your second part,

Double a = 2;

Doesn't work since 2 is not a double and the auto boxing only works between the same types. In this case 2 is an int.

But if you cast it.

Double a = (double)2;

works just fine.

double a = 2;

works because an int can be automatically converted to a double. But going the other way doesn't work.

int a = 2.2; // not permitted.

Check out the Section on conversions. In the Java Language Specification. Warning that it can sometimes be difficult to read.

Amended Answer.

In java you can cast up or down or have narrowing or widening casts (going from a 32 bit to 16 bit) value is narrowing. But I tend to think about it is losing vs not losing something. In most cases if you have the potential to lose part of value in assignment, you need to cast, otherwise you don't (See exceptions at end). Here are some examples.

long a = 2; // 2 is an integer but going to a long doesn't `lose` precision.
int b = 2L; // here, 2 is a long and the assignment is not permitted.  Even 
            // though a long 2 will fit inside an int, the cast is still 
            // required.
int b = (int)2L;  // Fine, but clearly a contrived case

Same for floating point.

float a = 2.2f; // fine
double b = a;   // no problem, not precision lost
float c = b;    // can't do it, as it requires a cast.

double c = 2.2f; // a float to a double, again a not problem.
float d = 2.2;  // 2.2 is a double by default so requires a cast or the float designator.
float d = (float)2.2;

Exceptions

No cast is required when converting from int to float or long to double. However, precision can still be lost since the floats only have 24 bits of precision and doubles only have 53 bits of precision.

To see this for ints you can run the following:

        for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE-100; i--) {
            float s = i;
            int t = (int)s; // normal cast required
            if (i != t) {
                System.out.println (i + " " + t);
            }
        }
2 of 2
0

Double is a wrapper class, creating a new Double casts a primitive variable of the SAME type into a Object. For Double h = 2, you are wrapping a int into a Double. Since wrapping only works between same types, if you want your Double variable be 2, then you should use

Double h = 2.0;

Discussions

eclipse - displaying Java double value - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
April 2, 2015
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
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.

🌐
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.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java double – tutorial with programming examples
Java Double - Tutorial With Programming Examples
April 1, 2025 - This is again a special Java class that provides simple arithmetic operations on the number (add, subtract, multiply and divide), rounding off the result, format conversion, and so on. Let’s look at the below example to understand this better. ... In the below example, we have demonstrated the difference between the simple subtraction of decimal and the subtraction through the Big-Decimal class. We have initialized two double variables and calculated the difference between their values.
🌐
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....
🌐
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.
Find elsewhere
🌐
DataCamp
datacamp.com › doc › java › double
double Keyword in Java: Usage & Examples
The double keyword in Java is a primitive data type that represents a double-precision 64-bit IEEE 754 floating point.
🌐
YouTube
youtube.com › watch
Java Double Tutorial: What is a Double in Java? Doubles Explained for Beginners & Experienced - YouTube
Java Double Tutorial: What is a Double in Java? Doubles Explained for Beginners & ExperiencedJava Double Tutorial: Master Decimal NumbersUnderstanding the Ja...
Published   July 11, 2025
🌐
Delft Stack
delftstack.com › home › howto › java › double in java
Double in Java | Delft Stack
October 12, 2023 - This tutorial will discuss double and the difference between double and Double. Double is a primitive data type in Java, whereas Double is a wrapper class that can create double object value.
🌐
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.
🌐
Quora
quora.com › What-is-double-in-Java
What is (double) in Java? - Quora
Answer (1 of 4): Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin...
🌐
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?
A double is twice the size of a float — thus the term double. In Java, the Float and Double wrapper classes have two properties that return the upper and lower limits of float and double data types, MIN_VALUE and MAX_VALUE:
🌐
CodeGym
codegym.cc › java blog › java numbers › java double keyword
Java double keyword
February 19, 2025 - On the other hand, Double is essential if you need to store your numeric values in a data structure like an ArrayList<Double>, or if you want to set them to null to indicate the absence of a value (primitives can’t be null). This can also come in handy when calling methods that specifically require an object type. So if your code is heavily object-oriented or you need the optional null state, Double might be your go-to choice.
🌐
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.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › double_keyword_in_java.htm
Java - double Keyword (double Data Type)
Python TechnologiesDatabasesComputer ... All Categories ... Java Vs. C++ ... The double keyword is used to define a double-type variable that stores a double value (which is a floating-type value)....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-double-class-java
Java.Lang.Double Class in Java - GeeksforGeeks
July 23, 2025 - Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 05 › difference-between-float-and-double-in-java.html
Difference between float and double variable in Java? Example
July 26, 2023 - Eclipse · jQuery · Java IO · Java XML · Though both float and double datatype are used to represent floating-point numbers in Java, a double data type is more precise than float.