[英]Long.valueOf not compiling for converting int to long
Looking at the answers to this question (link: How do I convert from int to Long in Java? ), I used the following to compare long values (newUpdate, lastUpdate) with int value (Interval) 看这个问题的答案(链接: 如何在Java中从int转换为Long? ),我使用以下方法将long值(newUpdate,lastUpdate)与int值(间隔)进行比较。
if ((newUpdate - lastUpdate) > Long.valueOf(interval))
I am not able to compile. 我无法编译。 What is the correct way to compare two different types?
比较两种不同类型的正确方法是什么?
More Info: 更多信息:
[INFO] Trace org.apache.maven.BuildFailureException: Compilation failure [145,51] operator > cannot be applied to long,java.lang.Long
Interval is of the type int
. 时间间隔是
int
类型的。
If interval
is an int
, then just ... 如果
interval
是一个int
,则只需...
if ((newUpdate - lastUpdate) > interval) { ...
There is NO good reason to explicitly convert interval
to a long
in this context. 在这种情况下,没有充分的理由将
interval
显式转换为long
。 The conversion will be done for you anyway. 无论如何,转换将为您完成。
The expression Long.valueOf(Interval)
returns a java.lang.Long
rather than a long
. 表达式
Long.valueOf(Interval)
返回一个java.lang.Long
而不是long
。 If you needed to explicitly turn interval
into a long
, then you should cast it: 如果您需要将
interval
明确转换为long
,则应将其强制转换为:
if ((newUpdate - lastUpdate) > ((long) interval)) {
The only thing that puzzles me about your example is why you get a compilation error when comparing a long
and a Long
. 唯一令我困惑的例子是,为什么在比较
long
和Long
时会出现编译错误。 The Long
should be auto-unboxed and the long
, long
version of the >
operator should be used: reference JLS - 15.20.1 which says that numeric relational operators perform unboxing if required. Long
应该自动拆箱,并且应该使用>
运算符的long
, long
版本:参考JLS-15.20.1 ,它表示如果需要,数字关系运算符可以执行拆箱。 The only explanation can be that you are compiling with the source level at 1.4 or earlier. 唯一的解释可能是您使用1.4或更早版本的源代码进行编译。
Long.valueOf(Interval)
retuns wrapper object Long
hence >
operator is not applicable. Long.valueOf(Interval)
包装对象retuns Long
因此>
操作者是不适用的。
If Interval
is String type then use Long.valueOf(Interval).longValue()
in your comparison. 如果
Interval
是String类型,则在比较中使用Long.valueOf(Interval).longValue()
。
If Interval
is int
type then just use Interval
in your comparison. 如果
Interval
是int
类型,则只需在比较中使用Interval
。 It will be auto promoted to long
--> specs . 它将自动升级为
long
规格 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.