50932

How use relational operators with number generics?

Question:

How can I use relational operators with Java number generics?

public class Test<K extends Number>{ private K key; public boolean f (int i){ return i < key; //ERROR } public boolean g (K k){ return k < key; //ERROR } }

Is there any solution for it? compareTo?

Answer1:

The easiest solution would be marking K to extend Number and implement Comparable<K>:

public class Test<K extends Number & Comparable<K>>{ private K key; public boolean f (int i) { if (!(key instanceof Integer)) { return false; } return new Integer(i).compareTo((Integer)key) < 0; } public boolean g (K k){ return k.compareTo(key) < 0; } }

Answer2:

The <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.20.1" rel="nofollow">Java Language Specification</a> states

<blockquote>

The type of each of the operands of a numerical comparison operator <strong>must be a type that is convertible (§5.1.8) to a primitive numeric type</strong>, or a compile-time error occurs.

</blockquote>

and

<blockquote>

A type is said to be <strong>convertible to a numeric type</strong> if it is a numeric type (§4.2), or it is a <strong>reference type that may be converted to a numeric type by unboxing conversion.</strong>

</blockquote>

These <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8" rel="nofollow">reference types are</a>

<blockquote>

From type Boolean to type boolean

From type Byte to type byte

From type Short to type short

From type Character to type char

From type Integer to type int

From type Long to type long

From type Float to type float

From type Double to type double

</blockquote>

Since all you know about K is that it is a subclass of Number and therefore not guaranteed to be any of the above, you cannot use a reference of type K as an operand of the < operator.

You would have to devise a strategy with the Comparable interface depending on what you are trying to achieve and how types should be compared.

Recommend

  • Performance and static method versus public method
  • How can a class method access a private member of another instance of the same class?
  • What’s the most idiomatic way to implement a “floating tag” in Git?
  • Is there a Java library that will create a number range from a list of numbers?
  • synchronization on single statement?
  • How can I compare two values in Java
  • IllegalArgumentException on Collections.sort() method
  • PHP: Overriding parent methods with __call
  • Best Way to create configuration file(config.php) php
  • Java Sort functions
  • How can we extract the main verb from a sentence?
  • How to prevent TreeItem selection?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • UIimage to char* conversion
  • Easiest way to covert part of a byte array to uint16
  • MySql - get days remaining
  • How to sort a same column both in asc order and desc order
  • css background images not always displayed
  • How to set current CultureUI via XAML binding
  • Cannot invoke my method on the array type int[]
  • Scala: Function returning an unknown type
  • Symfony2 - Doctrine - no changeset in post update
  • mave 3.2 not able to access local nexus instance return 502 code
  • LiveData is abstract android
  • Find group of records that match multiple values
  • Why use database factory in asp.net mvc?
  • Django simple Captcha “No module named fields” error
  • Sencha Touch 2.0 Controller refs attribute not working?
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Excel - Autoshape get it's name from cell (value)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Check if a string to interpolate provides expected placeholders
  • Rearranging Cells in UITableView Bug & Saving Changes
  • RestKit - RKRequestDelegate does not exist
  • Circular dependency while pushing http interceptor
  • Traverse Array and Display in markup
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET