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")
}
}
Check out the Scala certification blog!
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
($) The 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" "$@"
!#
println("Hello Intellipaat")
Prepare yourself for the industry by going through this Top Spark Interview Questions!
or the following code:
#!/bin/bash
exec scala "$0" "$@"
!#
object HelloIntellipaat {
def main(args: Array[String]) {
println("Hello Intellipaat " + args.toList)
}
}
HelloIntellipaat.main(args)
Have a look at our blog about Strings in R.
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 about this Scala Course!
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 the 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")
Refer to our blog for detailed information about Scala Collection.
This program can be executed from a CMD shell by entering a command like the following one:
C:\My Programs>HelloIntellipaat.bat
Type scala in the command prompt to start the 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!