Tutorial by Examples

Place this code in a file named HelloWorld.scala: object Hello { def main(args: Array[String]): Unit = { println("Hello World!") } } Live demo To compile it to bytecode that is executable by the JVM: $ scalac HelloWorld.scala To run it: $ scala Hello When the Scala...
object HelloWorld extends App { println("Hello, world!") } Live demo By extending the App trait, you can avoid defining an explicit main method. The entire body of the HelloWorld object is treated as "the main method". 2.11.0 Delayed Initialization Per the official d...
Scala can be used as a scripting language. To demonstrate, create HelloWorld.scala with the following content: println("Hello") Execute it with the command-line interpreter (the $ is the command line prompt): $ scala HelloWorld.scala Hello If you omit .scala (such as if you simply...
When you execute scala in a terminal without additional parameters it opens up a REPL (Read-Eval-Print Loop) interpreter: nford:~ $ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66). Type in expressions for evaluation. Or try :help. scala> The REPL allows ...
DescriptionCodeAssign immutable int valueval x = 3Assign mutable int valuevar x = 3Assign immutable value with explicit typeval x: Int = 27Assign lazily evaluated valuelazy val y = print("Sleeping in.")Bind a function to a nameval f = (x: Int) => x * xBind a function to a name with expl...

Page 1 of 1