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!
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).
您不能将基元用作类型参数。
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.