May 14, 2021 Scala
We can execute Scala code and compile it
scalac
the scalac command line tool.
object HelloWorld {
def main(args: Array[String]) {
println("Hello,World!")
}
}
The sign at the end of the statement is usually optional.
The sign at the end of the statement is usually optional.
Scala program processing starts with the main method, which is a mandatory part of each Scala program.
The primary method is not marked as static.
The primary method is the instance method for the automatic instantiation of single-case objects.
No type was returned.
There is actually Unit, which is similar to void, but it is inferred by the compiler.
We can explicitly specify the return type by adding a colon and type after the argument:
def main(args: Array[String]) : Unit = { }
Scala uses the
def
to tell the compiler that this is a method.
There are no access-level modifiers in Scala.
Scala does not specify a public modifier because the default access level is public.
Let's write a program that prints numbers from 1 to 10 in Print1.scalafile:
object Main {
def main(args: Array[String]) {
for {i <- 1 to10}
println(i)
}
}
We can enter
scala Main.scala
to run the code
The program assigns numbers 1 to 10 to variables, then performs println(i) and prints numbers 1 to 10.
In the Print2.scala file, put in
object Main {
def main(args: Array[String]) {
for {
i <- 1 to 10
j <- 1 to 10
}
println(i* j)
}
}