【发布时间】:2015-04-05 20:23:27
【问题描述】:
我有一个接收long 类型参数的方法,我尝试通过1 调用它:
contato.setId(1);
我收到了这个:
The method setId(Long) in the type Contato is not applicable for the arguments (int).
但是,1 不也是一个长数字吗?不是在长范围之内吗?
PS:顺便说一句,我用这段代码解决了问题:
Integer y = 1;
long x = y.longValue();
contato.setId(x);
这只是一个教学问题。
【问题讨论】:
-
如果该方法接受了 little-l
long,它就可以正常工作;只是 int 文字不能直接自动装箱到Long。 -
How did a float turn into a double here? 的可能重复项 - 我的回答解释了 Louis 评论中提到的自动装箱限制背后的决定
标签: java casting int long-integer primitive-types