在java中为int包装类指定一个int文字 [英] Assigning an int literal to a long wrapper class in java

查看:141
本文介绍了在java中为int包装类指定一个int文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Long l = 3 时出现编译错误而不是 Long l = 3L

Why is there a compilation error with Long l = 3 and not with Long l = 3L?

原始数据类型 long 同时接受 3 3升。我知道 3 是一个 int 文字 - 但不能分配给包装器对象? int 只有32位不适合64位整数类型吗?

The primitive data type long accepts both 3 and 3l. I understand that 3 is an int literal - but it can't be assigned to a Long wrapper object? int is only 32 bits shouldn't it fit in a 64 bit integer type?

推荐答案

因为没有 int 加宽自动装箱转换,自动装箱从转换为(但首先该值必须加宽 int long )。你可以做 3L ,或者

Because there isn't an int to Long widening and autoboxing conversion, autoboxing converts from long to Long (but first the value must be widened from an int to a long). You could do 3L as you have, or

Long l = Long.valueOf(3);

Long l = (long) 3;

这篇关于在java中为int包装类指定一个int文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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