Skip to content
Related Articles

Related Articles

Scala | Null, null, Nil, Nothing, None, and Unit
  • Last Updated : 10 Apr, 2019

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 :




    // 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)
        }
    }

    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 :




    // 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)
        }
    }

    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:




    // 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)
        }
    }

    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 :




    // 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)
        }
    }

    Output :

    List()

    Here, an empty List is returned.

    Example :




    // 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)
        }
    }

    Output :



    true

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

    Example :




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

    Output :

    None

    Hence, None can be converted to a String.

    Example :




    // 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)
        }
    }

    Output :

    None

    For more about this example see Scala’s Option.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :