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
// 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
// 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
// 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
// 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
// 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
// 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
// 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.
Similar Reads
Scala Programming Language Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides support to the functional programming approach. Scala programs can convert to bytecodes and can run on the JVM (Java Virtual Machine). Scala stands for S
3 min read
Scala Tutorial â Learn Scala with Step By Step Guide Scala is a general-purpose programming language that combines both object-oriented and functional programming. Designed for scalability and flexibility, it allows developers to write concise and maintainable code for everything from small scripts to large enterprise systems. First released in June 2
15+ min read
Introduction to Scala Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides the support to the functional programming approach. There is no concept of primitive data as everything is an object in Scala. It is designed to express
7 min read
Scala | flatMap Method In Scala, flatMap() method is identical to the map() method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. It can be defined as a blend of map method and flatten method. The output obtained by running the map method followed by the f
4 min read
For Loop in Scala In Scala, for loop is also known as for-comprehensions. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. Syntax: for(w <- range){ // Code.. } Here, w is
6 min read
Scala vs Java Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, etc. Java applications are compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. Scala is a general-purpose, high-level, multi-paradigm program
2 min read
Scala Console | println, printf and readLine Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs. Let's
2 min read
Hello World Program : First program while learning Programming In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a
6 min read
Class and Object in Scala Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real-life entities. Class A class is a user-defined blueprint or prototype from which objects are created. Or in other words, a class combines the fields and methods(member function which defines actions)
5 min read
Scala List filter() method with example The filter() method is utilized to select all elements of the list which satisfies a stated predicate. Method Definition: def filter(p: (A) => Boolean): List[A] Return Type: It returns a new list consisting all the elements of the list which satisfies the given predicate. Example #1: Scala // Sca
1 min read