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

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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program using Null and null
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
            // Method that takes a parameter of type Null
            def usingnull(thing: Null): Unit = 
            
                println("GeeksForGeeks"); 
            }
      
            /*error: type mismatch;
      
            found   : java.lang.String("hey")
      
            required: Null*/
            //usingnull("hey")
      
            // passing null itself
            usingnull(null)
        }
    }

    chevron_right

    
    

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program using Unit type
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
            // Method return type is unit
            def printNumber(num: (Int) => Unit) = 
            {
      
                num(1); 
                num(2); 
                num(3);
            }
              
            printNumber(println)
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to show that
    // Nil is an empty list
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Displays empty list
            println(Nil)
        }
    }

    chevron_right

    
    

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to convert
    // None to empty list
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Displays empty list
            println(None.toList)
        }
    }

    chevron_right

    
    

    Output :

    List()

    Here, an empty List is returned.

    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to test if
    // None is empty or not
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Displays true if empty
            // else false
            println(None.isEmpty)
        }
    }

    chevron_right

    
    

    Output :


    true

    Thus, we can say that None is empty as true is returned.

    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to convert None
    // to String
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Displays String
            println(None.toString)
        }
    }

    chevron_right

    
    

    Output :

    None

    Hence, None can be converted to a String.

    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of utilizing
    // None with Scala's Option
      
    // Creating object
    object GfG
    {
      
        // Main method
        def main(args: Array[String]) 
        {
            // Applying None with 
            // Scala's Option
            val p: Option[String] = None
      
            // Displays output
            println(p)
        }
    }

    chevron_right

    
    

    Output :

    None

    For more about this example see Scala’s Option.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.