What you are trying here is called unboxing, automatically converting an object to a primitive type (another way is autoboxing).
Java docs have the following:
The Java compiler applies decompression when an object of a wrapper class:
- Passed as a parameter to a method that expects a value of the corresponding primitive type.
- Assigned to a variable of the corresponding primitive type.
So one possibility is that you are not doing one of these things, and although at first glance it seems that you are not passing your modal expression to the method and not assigning it to a variable, it really is, at least in Java 6 :
class Test { public static void main(String args[]) { Integer x = 17; Integer y = 5; System.out.println (x % y); String [] z = new String[10]; z[x % y] = "hello"; } }
Another possibility is that you are using a pre-Java 5 environment where autoboxing and unpacking have been introduced.
The best bet in this case is probably to be explicit and use Integer.intValue() to get the base int .
However, you can also consider using int (not Integer ) for the key and only box it where you need it (when you add it to Entry ). It may be faster to use a primitive type, although you should, of course, test it to be sure.
source share