top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
775 views
What is Null in Scala? What is null in Scala? What is difference between Null and null in Scala?
posted Jul 13, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

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.

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.

scala> val myInt : Int = null
<console>: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.
Credits : http://www.journaldev.com/8958/scala-basic-interview-questions

answer Jul 17, 2016 by Shivam Kumar Pandey
...