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 :
object GfG
{
def main(args : Array[String])
{
def usingnull(thing : Null) : Unit =
{
println( "GeeksForGeeks" );
}
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 :
object GfG
{
def main(args : Array[String])
{
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:
object GfG
{
def main(args : Array[String])
{
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 :
object GfG
{
def main(args : Array[String])
{
println(None.toList)
}
}
|
Output :
List()
Here, an empty List is returned.
Example :
object GfG
{
def main(args : Array[String])
{
println(None.isEmpty)
}
}
|
Output :
true
Thus, we can say that None is empty as true is returned.
Example :
object GfG
{
def main(args : Array[String])
{
println(None.toString)
}
}
|
Output :
None
Hence, None can be converted to a String.
Example :
object GfG
{
def main(args : Array[String])
{
val p : Option[String] = None
println(p)
}
}
|
Output :
None
For more about this example see Scala’s Option.
Last Updated :
10 Apr, 2019
Like Article
Save Article