Just remember 3rd rule from the above link, widening + boxing is not allowed.
Generally when you write
It is converted by compiler to
But when you write
Then compiler should use this function of Long class for autoboxing:
here Long.valueOf(long) is taking long argument and in Long l=5; we are giving int argument.
Means the flow for this autoboxing is going through widening first.
Means int 5 needs to be converted to long 5, then long 5 should be autoboxed to Long wrapper class.
And java compiler does not support widening + boxing. Same allows for Double.valueOf(double) and Float.valueOf(float).
Dejan Miler wrote:Hello
Number 5 or any decimal number is of integer type (integer literal).
First of all this is legal because byte, short and char are of type int
I thought wrapper class Byte, Short, Char, Long work the same way, but Byte, Short, Char accept integer type without any kind of casting, which confuses me.
If default decimal number type is long, I think
Integer i = 5
will be legal.
Thank all of you
subject: question about primitive type and wrapper class