×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala Unit Data Type

By IncludeHelp Last updated : October 07, 2024

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!

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.