如何在Java中从int转换为Long? [英] How do I convert from int to Long in Java?
问题描述
我一直在寻找在长
到 int
遇到麻烦的Google用户,而不是其他方式周围。但我相信我不是唯一一个遇到这种情况之前从 int
到长
。
I keep finding both on here and Google people having troubles going from long
to int
and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int
to Long
.
我发现的唯一其他答案是只是设置为Long在第一位,这真的不解决问题。
The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question.
有人可以帮助我吗?我最初尝试铸造,但我得到一个无法从int转到长
Can someone help me out here? I initially tried casting but I get a "Cannot cast from int to Long
"
for (int i = 0; i < myArrayList.size(); ++i ) {
content = new Content();
content.setDescription(myArrayList.get(i));
content.setSequence((Long) i);
session.save(content);
}
你可以想象我有点困惑, int,因为一些内容是作为一个ArrayList进入的,并且我存储这个信息的实体需要序列号作为Long。
As you can imagine I'm a little perplexed, I'm stuck using int since some content is coming in as an ArrayList and the entity for which I'm storing this info requires the sequence number as a Long.
谢谢!
推荐答案
请注意,转换 long
和转换 Long
。如果你转换到 long
(一个原始值),那么它应该被自动装箱到 Long
Note that there is a difference between a cast to long
and a cast to Long
. If you cast to long
(a primitive value) then it should be automatically boxed to a Long
(the reference type that wraps it).
您可以使用 new
创建 Long
,用 int
值初始化它。
You could alternatively use new
to create an instance of Long
, initializing it with the int
value.
这篇关于如何在Java中从int转换为Long?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!