score:26
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):
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
.
Source: stackoverflow.com
Related Query
- Why in Scala Long cannot in initialized to null whear as Integer can
- Why an Int cannot be initialized as null in Scala?
- Why can a Scala trait extend a class?
- Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?
- Why can auxiliary constructors in Scala only consist of a single call to another constructor?
- Why it took so long to download sbt dependencies when I create a scala project in intellij idea?
- Why shouldn't one make every Scala instance variable a lazily initialized one?
- Cannot convert string to a long in scala
- Why can I assign null to a Unit value and why does it get converted to ()?
- Why can I use a Java lib from the Scala REPL but not from a script?
- Why can I have an impossible case in the scala match?
- Why does this code typecheck in Scala 2.11 and what can I do about it?
- Cannot create a tuple containing a null in Scala
- Why can I call += with a single parameter on a Scala Set?
- Why didn't scala design around Integer Overflow?
- why yield can not work with while loop in scala
- Why can scala not Infer that Left[X,A] is a reasonable subtype of Either[X,B]?
- How to implicitly wrap a value that can be null or an array into an Scala Option
- Min value of Integer List when it can be empty in scala
- Why does Scala separate symbols and letters in identifiers, and can I change this?
- Spark & Scala - Cannot Filter null Values from RDD
- Why does the Scala IntelliJ plugin take so long to start my application?
- Why are implicit variables not initialized in Scala when called from unit test?
- Can someone explain why applying map like this in Scala throws and IllegalArgumentException?
- Scala XML \\ copies xmlns attribute. Why and how can I stop it?
- why can scala support this: new PrintWriter("filename") { write("file contents"); close }?
- Why does Scala compiler fail with "object SparkConf in package spark cannot be accessed in package org.apache.spark"?
- Scala - can 'this' be null in Scala for a live object?
- Why can Scala and sbt compile this code?
- Why scala cannot infer common return type for a function with multiple parameter lists?
More Query from same tag
- Not found : Type Build (Unresolved 'Build')
- Scala ZIO Ref datatype
- Scala Pattern Matching with tuple
- Scala client composition with Traits vs implementing an abstract class
- Playframework, where to put Json Writes and Reads for reuse?
- scala error: constructor cannot be instantiated to expected type;
- Define a single trait out of multiple traits constrained by self-types
- is spark streaming able to store data in a database for every batch?
- Using a feeder to pass in header values (Gatling)
- Using scala to accumulate elementwise sums
- Transforming data from a runtime storage to a case class
- how to print Scala user defined Arrays
- Scala and akka lifecycle.. again
- How to create custom assignment operator in Scala
- How do I control order of execution of exec statements in Gatling scenario?
- How to package JARs in Spark (Scala) outside of a project?
- Adding element at the middle of scala.collection.immutable.Vector in Java
- Scala: jaxb or similar?
- Using snake case without explicit Configuration dependency in Circe
- How to batch insert to two tables using anorm with foreign key
- sbt multi-project build with test dependency between projects?
- accumulate the element ahead of nth element in list
- Invert cases within defined region
- Define custom serialization with Casbah / Salat - or delegate serialization to member?
- Scala functional programming dry run
- How to reference own class in scala?
- Implement trait with type parameters in method
- Dependent type with reference back (Scala)
- Android Intents and Scala
- SBT plugin: add resources to resulting jar?