0 votes
in Scala Constructs by

What is Null in Scala? What is null in Scala? What is difference between Null and null in Scala?

1 Answer

0 votes
by
Null is a Type (final class) in Scala. Null type is available in “scala” package as “scala.Null”. It has one and only one instance that is null. In Scala, “null” is an instance of type scala.Null type. Example:-

scala> val myNullRef : Null = null

myNullRef: Null = null

We cannot assign other values to Null type references. It accepts only ‘null’ value. Null is a subtype of all Reference types. Null is at the bottom of the Scala Type System. As it is NOT a subtype of Value types, we can assign “null” to any variable of Value type. Example:-

scala> val myInt : Int = null

:10: error: an expression of type Null is ineligible for implicit conversion

       val myInt : Int = null

^ Here type mismatch error. found : Null(null) but required: Int. The implicit conversions between Null and Int are not applicable because they are ambiguous.

Related questions

0 votes
asked Apr 21, 2022 in Scala Constructs by sharadyadav1986
0 votes
asked Apr 21, 2022 in Scala Constructs by sharadyadav1986
...