“Hello World..!” – Probably the first message that any developer tries to print on the console, while learning a new programming language.
Let’s write a basic Scala program to print “Hello World..!” message on the console and go through each keyword in the program.
1 2 3 4 5 6 |
object HelloWorld { def main(args: Array[String]) { println("Hello World!") } } |
Output:
Let’s go through each line of the program
1 |
object HelloWorld { |
An object called HelloWorld is defined with this statement.
1 |
def main(args: Array[String]) |
Similar to Java, main() is the starting point of the program execution in Scala. A function is defined in Scala as mentioned above.
“def” -> specifies we are going to define a function.
“main” -> function name
“args” -> parameter name
“Array[String]” -> parameter type which is Array of Strings.
1 |
println("Hello World!") |
We are calling println() method to print the string “Hello World!” to the output. By default, the output is printed on the console as this functions calls Java “System.out.println()” internally.
Happy Learning :).
Please leave a reply in case of any queries.