VOOZH about

URL: https://www.geeksforgeeks.org/scala/scala-null-null-nil-nothing-none-and-unit/

⇱ Scala | Null, null, Nil, Nothing, None, and Unit - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scala | Null, null, Nil, Nothing, None, and Unit

Last Updated : 11 Jul, 2025
The Empty values in Scala are represented by Null, null, Nil, Nothing, None, and Unit. The explication of these empty values are as follows:
  • null: The reference types such as Objects, and Strings can be nulland the value types such as Int, Double, Long, etc, cannot be null, the null in Scala is analogous to the null in Java.
  • Null: It is a Trait, which is a subset of each of the reference types but is not at all a sub-type of value types and a single instance of Null is null. The reference types can be assigned null but the value types cannot be assigned null. Example : Output :
    GeeksForGeeks
    Here, method usingnull that consists a parameter of type Null, we can only pass two things. null itself or a reference of type Null. when we passed a string as argument it didn't work and generated an error.
  • Nothing: Nothing is also a Trait, which has no instances. It is a subset of each of the distinct types. The major motive of this Trait is to supply a return type for the methods which consistently throws an exception i.e, not even a single time returns generally. It is also helpful in providing a type for Nil.
  • Unit: The Unit is Scala is analogous to the void in Java, which is utilized as a return type of a functions that is used with a function when the stated function does not returns anything. Example : Output :
    1
    2
    3
    Here, method printNumber takes a parameter called num, which has a type of (Int) => Unit. This means that num is a method that consists a single parameter of type Int. method printNumber return type of Unit, which means num isn’t supposed to return a value.
  • Nil: Nil is Considered as a List which has zero elements in it. The type of Nil is List[Nothing] and as stated above, that Nothing has no instances, we can have a List which is confirmed to be desolated. Example: Output :
    List()
    Thus, we can see that an empty list is returned.
  • None: It is one of the children of Scala's Option class which is utilized to avoid assignment of null to the reference types. lets see some examples. Example : Output :
    List()
    Here, an empty List is returned. Example : Output :
    true
    Thus, we can say that None is empty as true is returned. Example : Output :
    None
    Hence, None can be converted to a String. Example : Output :
    None
    For more about this example see Scala's Option.
Comment

Explore