A cheatsheet that gets you started. The original tutorial can be found here
Main function The normal version:
object Hello { def main(args: Array[String]) = { println("Hello, world") } } The version that extends App trait
object Hello extends App { println("Hello, world") } // To access args. object Hello extends App { if (args.size == 0) println("Hello, you") else println("Hello, " + args(0)) } As you may notice: no ; needed as ending in Scala.
Variables The type in declaration is optional. val s: String = "hello" // val for immutable, final in Java. val s = "hello" // Same as above. var i: Int = 42 // var for mutable. var i = 42 // Same as above. Built-in types: val b: Byte = 1 // 8-bit signed [-128, 127](-2^7 to 2^7-1, inclusive) val s: Short = 1 // 16-bit signed. val x: Int = 1 // 32-bit signed. val l: Long = 1 // 64-bit signed. val f: Float = 3.0 // 32-bit IEEE 754 single-precision float. val d: Double = 2.0 // 64-bit IEEE 754 double-precision float. val c: Char = 'a' // 16-bit unsigned Unicode character. val s: String = "str" // A sequence of Char BigInt and BigDecimal for large numbers:
...