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)
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.
If
intervalis anint, then just …There is NO good reason to explicitly convert
intervalto alongin this context. The conversion will be done for you anyway.The expression
Long.valueOf(Interval)returns ajava.lang.Longrather than along. If you needed to explicitly turnintervalinto along, then you should cast it:The only thing that puzzles me about your example is why you get a compilation error when comparing a
longand aLong. TheLongshould be auto-unboxed and thelong,longversion of the>operator should be used: reference JLS – 15.20.1 which says that numeric relational operators perform unboxing if required. The only explanation can be that you are compiling with the source level at 1.4 or earlier.