简体   繁体   English

Long.valueOf无法编译,无法将int转换为long

[英]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 . 唯一令我困惑的例子是,为什么在比较longLong时会出现编译错误。 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 应该自动拆箱,并且应该使用>运算符的longlong版本:参考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. 如果Intervalint类型,则只需在比较中使用Interval It will be auto promoted to long --> specs . 它将自动升级为long 规格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 性能:关于使用 Long.valueOf(..) - Java Performance: regarding using Long.valueOf(..) Long a = Long.valueOf(1)或Long a = 1L之间有什么区别吗? - Is there any difference between Long a = Long.valueOf(1) or Long a = 1L? 为什么Long.valueOf(0).equals(Integer.valueOf(0))为false? - Why is Long.valueOf(0).equals(Integer.valueOf(0)) false? 为什么使用Long.valueOf(...)而不是长文字? - Why use Long.valueOf(…) rather than a long literal? 调用TreeSet时发生ClassCastException <Long> .contains(Long.valueOf(someLongValue)) - ClassCastException when calling TreeSet<Long>.contains( Long.valueOf( someLongValue ) ) Java中Long.valueOf(0)和0L的区别是什么? - Which is the difference between Long.valueOf(0) and 0L in Java? 为什么 Double.valueof 可以处理“1D”而 Long.valueof 不能处理“1L”? - why Double.valueof can handle "1D" while Long.valueof cannot handle "1L"? Long.valueOf(java.lang.String)和new Long(java.lang.String)之间的区别? - Difference between Long.valueOf(java.lang.String) and new Long(java.lang.String)? 为什么在使用Base64加密给java.lang.ClassCastException时使用Long.valueOf(stateMap.get(“ time”)) - Why Long.valueOf(stateMap.get(“time”)) when using Base64 encrytion giving java.lang.ClassCastException 将long转换为int得出0 - Converting long to int gives 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM