Why is implicit conversion from int to Long not possible?

后端 未结 3 395
攒了一身酷
攒了一身酷 2021-01-05 02:14

I can implicitly conver int to long and long to Long. Why is it not possible to implicitly convert int to Long? Why can\'t Java do the implicit conversion on the last line o

3条回答
  •  清酒与你
    2021-01-05 02:30

    Long and Integer are objects. Boxing/unboxing only works with primitives. Doing Long boxedLong = i is like Long boxedLong = new Integer(10), that's a no no ! Plus, remember that there is no inheritance between Long and Integer so even Integer i = new Long() is not valid

提交回复
热议问题