With the comments it is clear that there is a confusion between Boxing and casting. They are completely different concepts.
Boxing
What the guy Double
makes is the Boxing of floating point values with double precision. It does not convert, does not promote anything. It takes this value and places it within a class called Double
.
This class is a type by reference, so the variable that will contain the value has a pointer to the object where the data is. The stored value is there in the object and can only be accessed indirectly through the pointer. The compiler already does this by the programmer.
This is what is called boxing a value into an object. There is nothing conversion here.
Casting
Casting is a process of promoting from one type to another. In some cases it is only to indicate to the compiler that the value should be interpreted as if it were another. In another it is necessary to do a conversion, since its internal structure is different. When it is possible to do it safely the compiler applies the casting automatically in some situations preestablished by language.
I talked about it in What is upcasting and downcasting in the Java language?.
This is because there are rules in the language of self-promotion of compatible types only among the primitive types of language*. Works:
double d1 = 10;
double
is a "primitive type". Double
it is not, this is already known. So in class use there is no promotion of an entire number for the Double
. There is no rule that determines what must be done.
They could have defined it in the language, but they did not and it makes sense not to do it in the language itself. If they had, I could have had a cast implicit in the class Double
. This, of course, if Java allowed the definition of operators in the classes.
It is possible to make a cast explicit, then everything ok:
Double d2 = (double)10;
It is possible to pass a value that is already a double
through literals:
Double d3 = 10.0;
Double d4 = 10d;
Or through a constructor or another method (used in the question) that creates a double-precision number:
Double d5 = new Double(10);
Note that the builder expects a double
and the whole promotion to this type occurs implicitly as per the language specification.
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
The Boxing for Double
expects a number of the type double
to box it, if it doesn’t get exactly this, it doesn’t work.
From the comments it seems to be difficult to understand this, so I will repeat: There is no rule that determines this automatic conversion, it only works if it is explicit.
It would be enough to have a rule that determined that this was done by the compiler that would work. The language developers thought it was not desirable, probably because they thought it could happen by accident.
*This idea of primitive will get complicated when Java has basic data so it is not primitive (and will soon). And I want to see how they’ll handle casting since they have decided not to have operator overload, will change their mind or will be something more capenga?
This answer responds directly and summarized what occurred there.
– user28595
@Diegof there occurred something a little different
– Maniero