-1

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.

Community
  • 1
  • 1
  • What is the compilation error? Is `Interval` of type `int` or `Integer`? – madth3 Dec 03 '12 at 14:13
  • [INFO] Trace org.apache.maven.BuildFailureException: Compilation failure [145,51] operator > cannot be applied to long,java.lang.Long – RedDopamine Dec 03 '12 at 14:16
  • Interval is of the type int. – RedDopamine Dec 03 '12 at 14:17
  • 1
    I would kindly ask you to refrain from starting variable names with uppercase letters. Please find additional information in the [Java Code Conventions, Chapter 9: Naming Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367). Thank you very much, you will help everyone reading your code not getting a myocardial infarction or developing suicidal tendencies. ;) – brimborium Dec 03 '12 at 14:27
  • I changed the Interval to interval. – RedDopamine Dec 03 '12 at 14:42

2 Answers2

3

If interval is an int, then just ...

    if ((newUpdate - lastUpdate) > interval) { ...

There is NO good reason to explicitly convert interval to a long in this context. The conversion will be done for you anyway.

The expression Long.valueOf(Interval) returns a java.lang.Long rather than a long. If you needed to explicitly turn interval into a long, then you should cast it:

    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. 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. The only explanation can be that you are compiling with the source level at 1.4 or earlier.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Long.valueOf(Interval) retuns wrapper object Long hence > operator is not applicable.

If Interval is String type then use Long.valueOf(Interval).longValue() in your comparison.

If Interval is int type then just use Interval in your comparison. It will be auto promoted to long -->specs.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • I used Long.valueOf(Interval).longValue() and it works fine now. I will try only Interval too. – RedDopamine Dec 03 '12 at 14:27
  • @RedDopamine - programming is not about trying different fixes until you find something that "works". You need to understand the Java language so that you *know* what your original mistake was, and can choose the right fix. – Stephen C Dec 04 '12 at 00:01