Reputation: 10295
I have this class Entry...
public class Entry <K, V> {
private final K mKey;
private final V mValue;
public Entry() {
mKey = null;
mValue = null;
}
}
What happens if I use an int as the mKey? As far as I know ints can't be null!
Upvotes: 1
Views: 203
Reputation: 72399
Generic type parameters need to be objects, they can't be primitives. So you can use the Integer
wrapper class around mKey / mValue and set it to null, but trying to use the int primitive will always give you a compilation error.
Upvotes: 0
Reputation: 81154
A variable of type Integer
can be null. An int
cannot be null. The latter is the primitive type, the former is a wrapper reference type for dealing with primitives as an Object. If you're using this:
Entry<Integer, String> myEntry;
Then you are necessarily using the wrapper type. Primitives can't be used as type parameters in Java so you can't have Entry<int, String>
for example (it won't compile).
Upvotes: 6