Java 中如何将 int 转换为 Long?
我一直发现这里和 Google 的人们在从 long
转换为 int
时遇到了麻烦,而不是相反。但我确信我不是唯一一个在从 int
转换为 Long
之前遇到过这种情况的人。
我发现的唯一其他答案是“首先将其设置为 Long”,这确实没有解决问题。
我最初尝试进行强制转换,但收到“无法从 int 强制转换为 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 形式传入,而我为其存储此信息的实体需要长整型序列号。
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
.
The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question.
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);
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
请注意,转换为
long
和转换为Long
之间存在差异。如果您转换为long
(原始值),那么它应该自动装箱为Long
(包装它的引用类型)。您也可以使用
new
创建Long
的实例,并使用int
值对其进行初始化。Note that there is a difference between a cast to
long
and a cast toLong
. If you cast tolong
(a primitive value) then it should be automatically boxed to aLong
(the reference type that wraps it).You could alternatively use
new
to create an instance ofLong
, initializing it with theint
value.使用以下内容:
Long.valueOf(int);
。Use the following:
Long.valueOf(int);
.如果您已经将 int 输入为 Integer,则可以执行以下操作:
If you already have the int typed as an Integer you can do this:
使用
或
use
or
怎么样
// 不会编译
// 编译,并保留 int 的非 NULL 精神。最好的演员就是完全没有演员。当然,您的用例可能需要 Long 值和可能的 NULL 值。但是,如果 int 或其他 long 是您唯一的输入,并且您的方法可以修改,我建议采用这种方法。
// 编译是最有效的方法,并且清楚地表明源值是并且永远不会是 NULL。
How About
// Will not compile
// Compiles, and retains the non-NULL spirit of int. The best cast is no cast at all. Of course, your use case may require Long and possible NULL values. But if the int, or other longs are your only input, and your method can be modified, I would suggest this approach.
// Compiles, is the most efficient way, and makes it clear that the source value, is and will never be NULL.
在Java中你可以这样做:
在你的情况下它将是:
In Java you can do:
in your case it would be:
我有这个小玩具,它也处理非通用接口。
如果输入错误,我可以接受抛出 ClassCastException (好的并且很高兴)
I have this little toy, that also deals with non generic interfaces.
I'm OK with it throwing a ClassCastException if feed wrong (OK and happy)
我们将通过使用
Number
引用来获取长值。它适用于所有数字类型,这是一个测试:
We shall get the long value by using
Number
reference.It works for all number types, here is a test:
我为此遇到了很大的麻烦。我只是想:
其中 IntervalCount 是 Long,并且 JSpinner 设置为返回 Long。最终我不得不编写这个函数:
这似乎可以解决问题。没有任何简单的铸造,上述解决方案都不适合我。非常令人沮丧。
I had a great deal of trouble with this. I just wanted to:
Where IntervalCount is a Long, and the JSpinner was set to return a Long. Eventually I had to write this function:
which seems to do the trick. No amount of simple casting, none of the above solutions worked for me. Very frustrating.
Android Studio lint 检查建议:删除不必要的装箱:所以,拆箱是:
Suggested From Android Studio lint check : Remove Unnecessary boxing : So, unboxing is :
除了这里建议的其他方法之外,还可以尝试下面的代码。
原始到原始。
Apart from the other ways suggested here, one can try the below code as well.
primitive to primitive.
只要只有方法
Long.valueOf(long)
,在使用的情况下,就会隐式地从
。int
转换为long
>Long.valueOf(intValue)更明确的方法是
As soon as there is only method
Long.valueOf(long)
, cast fromint
tolong
will be done implicitly in case of usingLong.valueOf(intValue)
.The more clear way to do this is