Scala Hello Program

Category : Java | Sub Category : | By Runner Dev Last updated: 2020-09-18 14:46:34 Viewed : 195


Scala Hello Program

package com.prabonam.main

 

object Runner {  //

   def main(args: Array[String]): Unit = {

     println("Hello Scala!")

   }

}

OutPut:

Hello Scala!

Explanation:

  • It defines a method named main inside a Scala object named Hello
  • An object is similar to a class, but you specifically use it when you want a single instance of that class
  • If you’re coming to Scala from Java, this means that main is just like   a static method (We write more on this later)
  • main takes an input parameter named args that is a string array
  • Array is a class that wraps the Java array primitive

 

·        object Runnerobject is the keyword which is used to create the objects. Objects are the instance of a class. Here “Runner” is the name of the object.

·        def main(args: Array[String]): def is the keyword in Scala which is used to define the function and “main” is the name of Main Method. args: Array[String] are used for the command line arguments.

·        println(“Hello Scala!”): printtewtln is a method in Scala which is used to display the Output on console.

 

scala.Unit

Unit is a subtype of scala.AnyVal. There is only one value of typtest dsiplay link Unit, (), and it is not represented by any object in the underlying runtime system. A method with return type Unit is analogous to a Java method which is declared void.

final abstract class Unit private extends AnyVal {

  override def getClass(): Class[Unit] = null

}

Search
Related Articles

Leave a Comment: