In Java, the usual way you did this would be with null
and Integer
, Long
, etc. classes (which are equivalents of a reference type for primitive types int
, Long
, etc., being reference types, references can be null
). If you have a C # background, Integer
in Java (with autoboxing) is kind of like int?
in c #.
For example, List#indexOf
has this signature:
int indexOf(Object o)
... and does -1
thing you are talking about. If you designed List
and preferred null
, you could define it as:
Integer indexOf(Object o)
... and returned null
, not -1
in the case of "not found".
There are versions of the reference type of all primitive types in Java, and, of course, all other types are already reference types, so you already have the null
option.
source share