Program Structure
In this page:
Program Structure
A basic Scala program is organized as an object containing a main method (or extending App), with statements executed in order. The object Main extends App shorthand lets you write top-level statements directly in the object's body instead of wrapping them in an explicit main method. Both styles compile to a runnable program.
Note: extends App is convenient for scripts and short programs, but explicit def main(args: Array[String]): Unit is more common in larger, production Scala code.
Example: Program Structure
object Main {
def main(args: Array[String]): Unit = {
println("Program started")
greet()
println("Program finished")
}
def greet(): Unit = {
println("Hello from a helper method!")
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: