JavaToScala

TODO for Marco, Andreas and Georg

Important to know

A semicolon at the end of a line is optional. Just use it if you want to use multiple statements on a single line

Note! x + y

is not x + y! Use (x + y) or x + y

Data-types and variables:

val is like final

Unit (Scala-World) is likely the same as void (Java-World)!

Array's:

Java: int a[size]; a[1] = 12;

Scala: var a: Array[Int] or var a = new Array[Int](size) a(1) = 12

Note! i++ or ++i will not work in Scala! Use i += 1 instead

Functions:

JAVA: int max(int a, int b) { if(a<b) return b   else return a }

Scala: def max(a: int, b: int) = { if(a<b) b   else a }

Note! Scala don't need 'return'. The last executed line is the return-value of the function

Imperative vs Functional Programming Imperative Style(C++, Java, ...): vars, methods with (unknown) side effects, and mutable objects(map, array, ...) Functional Style: using val most of the time, methods without side effects and immutable objects(map, list, ...)

Note! Container Classes like map and set can be both - immutable (map: default!) and mutable

Try to use functional Programming

Classes and Objects default access modifier is public - you need NOT to write public var a: Int

function parameters are always val! Changing the parameter value will not compile

keyword 'object' create a singleton class object compare it with statics(JAVA)

Companion classes and objects

If a class and a object with are located in the same source file, they are called companion class/object. A class and its companion object can access each other's PRIVATE members.