First Program in Scala

It includes both an interpreter and compiler. The compiler generates a .class file that is binary files that can be executed by the JVM whereas the interpreter executes source code contained in a text file or it can be used to work interactively with Scala. A simple program in scala is shown as follows:-

object HelloIntellipaat {
def main(args: Array[String]) {
println("Hello Intellipaat")
}
}

Want to get certified in Scala! Learn Scala from top Scala experts and excel in your career with Intellipaat’s Scala certification!

The identifier args refers to the command line arguments, main is predefined. Suppose this code is stored in HelloIntellipaat.scala. The command which is used to compile and execute the .class file is as follows:

$ scalac HelloIntellipaat.scala
$ scala HelloIntellipaat
Output

Hello Intellipaat
$

Dollar sign represents the command line prompt and -classpath specifies the location of one or more .class files. If the user wants to use the interpreter and is working on a UNIX system or a Unix-like system such as OpenSolaris or Linux respectively use the following code:

#!/bin/bash
exec scala "$0" "[email protected]"
!#
println("Hello Intellipaat")
 

Prepare yourself for the industry by going through this Top Apache Spark and Scala Interview Questions and Answers!

or the following code:

#!/bin/bash
exec scala "$0" "[email protected]"
!#
object HelloIntellipaat {
def main(args: Array[String]) {
println("Hello Intellipaat " + args.toList)
}
}
HelloIntellipaat.main(args)

in a text file, say HelloIntellipaat. The program can be executed by entering the following command:

$ hello

Interested in learning Scala? Click here to learn more in this ScalaTraining in New York!

The two versions presented do not produce exactly the same output. For example, the second version will print the following output
Hello Intellipaat List()
On Windows systems users can get same results by creating a text file say hello.bat which  contains the following code:

::#!
@echo off
call scala %0 %*
goto :eof
::!#
rem *
rem Scala code follows
rem *
println("Hello Intellipaat")


This program can be executed from a CMD shell by entering a command like the following one:
C:\My Programs>HelloIntellipaat.bat

Still, have queries? Come to Intellipaat’s Big Data Community, clarify all your doubts, and excel in your career!

Type scala in command prompt to start scala interpreter –

$ scala
Welcome to Scala version 2.11.7.final (Java HotSpot(TM) Server 64-Bit VM, Java 1.6.0_18).
Type in expressions to have them evaluated
Type: help for more information.
e.g.
scala> var x=20
x: Int = 20
scala> ("intellipaat").length
res1: Int = 11

This blog will help you get a better understanding of the Significance Of Scala!

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
11 ⁄ 11 =