为什么无法从int到Long隐式转换? [英] Why is implicit conversion from int to Long not possible?

查看:113
本文介绍了为什么无法从int到Long隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以隐式地将int转换为long,并将long转换为Long.为什么无法将int隐式转换为Long?为什么Java不能在示例的最后一行进行隐式转换?

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 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

推荐答案

Long Integer 是对象.装箱/拆箱仅适用于原语.做 Long boxedLong = i 就像 Long boxedLong = new Integer(10),那是不!另外,请记住, Long Integer 之间没有继承,所以即使 Integer i = new Long()也是无效的

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

这篇关于为什么无法从int到Long隐式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆