Why is implicit conversion from int to Long impossible?

I can implicitly convert int to long and long to Long. Why is it impossible to force an int to convert to Long? Why can't Java do implicit conversion in the last line of the example?

int i = 10; //OK
long primitiveLong = i;  //OK
Long boxedLong = primitiveLong;  //OK
boxedLong = i; //Type mismatch: cannot convert from int to Long
+4
source share
3 answers

Longand Integerare objects. Boxing / unboxing only works with primitives. Doing Long boxedLong = iit looks like Long boxedLong = new Integer(10)no no! Also, remember that between Longand Integerthere is no inheritance, so it’s Integer i = new Long()not even valid

+6
source

, long Long , , Long . , , Long , . , - , , , , . , , . ( Long vs long java?)

, . , int , long , long .

Long.valueOf()

:

Long longValue = Long.valueOf(InsertIntHere);
+1

. .

: Long.valueOf(int);

0

All Articles