Your first lines of code in Scala
The “Hello, world!” Program
As a first example, we use the standard “Hello, world!” program to demonstrate the use of the Scala tools without knowing too much about the language.
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
The structure of this program should be familiar to Java programmers: it consists of the method main
which prints out a friendly greeting to the standard output.
We assume that both the Scala software and the user environment are set up correctly. For example:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
main
which prints out a friendly greeting to the standard output.Environment | Variable | Value (example) |
---|---|---|
Unix | $SCALA_HOME | /usr/local/share/scala |
$PATH | $PATH:$SCALA_HOME/bin | |
Windows | %SCALA_HOME% | c:\Progra~1\Scala |
%PATH% | %PATH%;%SCALA_HOME%\bin |
Run it interactively!
The scala
command starts an interactive shell where Scala expressions are interpreted interactively.
> scala
This is a Scala shell.
Type in expressions to have them evaluated.
Type :help for more information.
scala> object HelloWorld {
| def main(args: Array[String]): Unit = {
| println("Hello, world!")
| }
| }
defined module HelloWorld
scala> HelloWorld.main(Array())
Hello, world!
scala>:q
>
The shortcut :q
stands for the internal shell command :quit
used to exit the interpreter.
scala
command starts an interactive shell where Scala expressions are interpreted interactively.
> scala
This is a Scala shell.
Type in expressions to have them evaluated.
Type :help for more information.
scala> object HelloWorld {
| def main(args: Array[String]): Unit = {
| println("Hello, world!")
| }
| }
defined module HelloWorld
scala> HelloWorld.main(Array())
Hello, world!
scala>:q
>
:q
stands for the internal shell command :quit
used to exit the interpreter.Compile it!
The scalac
command compiles one (or more) Scala source file(s) and generates Java bytecode which can be executed on any standard JVM. The Scala compiler works similarly to javac
, the Java compiler of the Java SDK.
> scalac HelloWorld.scala
By default scalac
generates the class files into the current working directory. You may specify a different output directory using the -d
option.
> scalac -d classes HelloWorld.scala
scalac
command compiles one (or more) Scala source file(s) and generates Java bytecode which can be executed on any standard JVM. The Scala compiler works similarly to javac
, the Java compiler of the Java SDK.
> scalac HelloWorld.scala
scalac
generates the class files into the current working directory. You may specify a different output directory using the -d
option.
> scalac -d classes HelloWorld.scala
Execute it!
The scala
command executes the generated bytecode with the appropriate options:
> scala HelloWorld
scala
allows us to specify command options, such as the -classpath
(alias -cp
) option:
> scala -cp classes HelloWorld
The argument of the scala
command has to be a top-level object. If that object extends trait App
, then all statements contained in that object will be executed; otherwise you have to add a method main
which will act as the entry point of your program.
Here is how the “Hello, world!” example looks like using the App
trait:
object HelloWorld extends App {
println("Hello, world!")
}
scala
command executes the generated bytecode with the appropriate options:
> scala HelloWorld
scala
allows us to specify command options, such as the -classpath
(alias -cp
) option:
> scala -cp classes HelloWorld
scala
command has to be a top-level object. If that object extends trait App
, then all statements contained in that object will be executed; otherwise you have to add a method main
which will act as the entry point of your program.App
trait:
object HelloWorld extends App {
println("Hello, world!")
}
Script it!
We may also run our example as a shell script or batch command (see the examples in the man pages of the scala
command).
The bash shell script script.sh
containing the following Scala code (and shell preamble):
#!/usr/bin/env scala
object HelloWorld extends App {
println("Hello, world!")
}
HelloWorld.main(args)
can be run directly from the command shell:
> ./script.sh
Note: We assume here that the file script.sh
has execute permission and the search path for the scala
command is specified in the PATH
environment variable.
scala
command).script.sh
containing the following Scala code (and shell preamble):
#!/usr/bin/env scala
object HelloWorld extends App {
println("Hello, world!")
}
HelloWorld.main(args)
> ./script.sh
script.sh
has execute permission and the search path for the scala
command is specified in the PATH
environment variable.Introduction to SCALA: Installing Scala Day 1 Learnings
Share this article with your friends.
No comments :
Post a Comment