Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
How to Check for Null in a Single Statement in Scala?
Next article icon

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

Last Updated : 10 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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.



Next Article
How to Check for Null in a Single Statement in Scala?
author
nidhi1352singh
Improve
Article Tags :
  • Scala
  • Scala
  • Scala-Basics
  • Scala-Keyword
  • scala-parameterized-type

Similar Reads

  • How to Check for Null in a Single Statement in Scala?
    In Scala, null is a Special type value that represents the absence of a value or a reference or the unavailability of a value for a variable. It is important to handle null values in programming otherwise it can lead to NullPointerException. In this article, we are going to see different examples th
    3 min read
  • How to check String is null in Scala?
    In this article, we will learn how to check if a string is null in Scala. In Scala, you can check if a string is null using the following methods: Table of Content 1. Using the == operator:2. Using the eq method (recommended):3. Using Pattern Matching:1. Using the == operator:[GFGTABS] Scala object
    2 min read
  • Lazy val and Infinite Sequences in Scala
    Vals and Lazy vals are present in Scala. lazy keyword changes the val to get lazily initialized. Lazy initialization means that whenever an object creation seems expensive, the lazy keyword can be stick before val. This gives it the advantage to get initialized in the first use i.e. the expression i
    3 min read
  • Scala | Methods to Call Option
    The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. There are a few methods that
    5 min read
  • How to define Optional Parameter in Scala?
    Optional parameters in Scala provide us with a procedure or a way to define some default values for function parameters. The need to define default values comes when a value is not provided for the parameter during function invocation. These are also useful when we want to provide some default or sp
    5 min read
  • Scala Float isNaN() method with example
    The isNaN() method is utilized to return true if this Float value or the specified float value is Not-a-Number (NaN), or false otherwise. Method Definition: (Float_Number).isNaN Return Type: It returns true if this Float value or the specified float value is Not-a-Number (NaN), or false otherwise. E
    1 min read
  • Scala short <(x: Long): Boolean
    Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Long) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Long): Boolean Return Type: It returns true if this value is less
    1 min read
  • Methods to call on a Set in Scala
    A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.
    13 min read
  • Scala Long !=(x: Long) method with example
    In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The !=(x: Long) method is utilized to check whether the given Long value is equal to each other or not. Method Definition - def !=(x: Long): Boolean Returns - Returns true if this value is not equal to x, f
    1 min read
  • Scala List length() method with example
    The length() method is utilized to find the length of the list. Method Definition: def length: Int Return Type: It returns the length of the list stated. Example: 1# // Scala program of length() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a lis
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences