Home »
Scala
Scala Unit Data Type
In this tutorial, we will learn about the Unit data type in Scala with examples.
By Shivang Yadav Last updated : April 02, 2023
Unit Type
The Unit type in Scala is used as a return statement for a function when no value is to be returned. Unit type can be e compared to void data type of other programming languages like Java. It is a subclass of anytype trait and is used when nothing means to return by the function.
Scala program to illustrate the working of Unit type
object MyClass {
def printval(a: String): Unit = {
println("Hello! "+a)
}
def main(args: Array[String]) {
val a = printval("IncludeHelp")
println("Value returned is "+a)
}
}
Output
Hello! IncludeHelp
Value returned is ()
Syntax to define a main() method with Unit type
def main(args: Array[String]) : Unit = {
}
Example of main() method with Unit type
object MyClass {
def main(args: Array[String]): Unit = {
println("Hello world!");
}
}
Output
Hello world!