score:26

Accepted answer

scala long is literal data type like long in java. same is true for int. but integer is a wrapper class i.e java.lang.integer

if you want nullable long value, you can use java.lang.long

score:4

in scala, type scala.null is a subtype of all reference types (all types which inherit from scala.anyref - which is an alias of java.lang.object). that's why you can use the null value (of type scala.null) anywhere a reference type is expected.

it is not, however, a subtype of any value type (types which inherit from scala.anyval). because types such as scala.int, scala.long, scala.boolean... (corresponding to java's primitive types) are value types, they cannot be null.

as for the integer type you mention: i guess this is java.lang.integer, which is a subtype of java.lang.object (a.k.a. scala.anyref): it's a reference type, so you can use null.

it's probably easier to understand with this diagram of the scala type hierarchy (lifted from the official documentation): scala types hierarchy

score:11

int and long are scala types and are derived from anyval, which is not a reference type (i.e. not anyref) and hence can't be null. integer on the other hand is an alias for java.lang.integer, which is a reference type.

no, it's not an oversight, int and long are consistent, using null in scala is considered a bad thing and, as i understand it, null exists merely for java compatibility (even for anyref) as well as integer.


Related Query

More Query from same tag