Scala language syntax and rules
Scala is a less strict and verbose language than Java: semicolons are optional; even parentheses on function calls are not required when not needed (note the val b = new AddDemoFunctional
line in the last example). We'll cover the following topics:
- Statically typed language
- Mutable and immutable variables
- Common Scala types
Statically typed language
Like Java, Scala is a statically typed language; you'll have to declare variables before you can use them. It is also a strongly typed language. You can always specify the used types, like you would in Java code. Unlike Java, you are not always required to specify the types explicitly, though.
Types must be specified when declaring method input parameters and the method's return value. Types are not required when declaring variables inside a method or function body, because the Scala compiler usually can detect the correct types from the code.
Here's an example of this:
var i = 10; var j = new java.lang.Object...