This document summarizes some of the key differences between Scala and Java syntax. It covers topics like type definitions, variables, methods, classes, traits, collections, exceptions, control flow, and packages. Overall, the document shows that Scala code is more concise and expressive than equivalent Java code for many common programming constructs.
4. Variables
Scala: Java:
val s = “Hello World” public final String s = “Hello World”;
var i = 1 public int i = 1;
private var j = 3 private int j = 3;
5. Methods
Scala: Java:
def add(x: Int, y: Int): Int = { public int add(int x, int y) {
x+y return x + y;
} }
def add(x: Int, y: Int) = x + y
def doSomething(text: String) { public void doSometing(String text) {
} }
8. Classes and constructors
Scala: Java:
class Person(val name: String) public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
9. Traits (= Interface + Mixin)
Scala: Java:
trait Shape { interface Shape {
def area: Double public double area();
} }
class Circle extends Object with public class Circle extends Object
Shape implements Shape
10. No “static” in Scala
Scala: Java:
object PersonUtil { public class PersonUtil {
val AgeLimit = 18 public static final int
AGE_LIMIT = 18;
def countPersons(persons:
List[Person]) = ... public static int
countPersons(List<Person>
}
persons) {
...
}
}
11. if-then-else
Scala: Java:
if (foo) { if (foo) {
... ...
} else if (bar) { } else if (bar) {
... ...
} else { } else {
... ...
} }
12. For-loops
Scala: Java:
for (i <- 0 to 3) { for (int i = 0; i < 4; i++) {
... ...
} }
for (s <- args) println(s) for (String s : args) {
System.out.println(s);
}
15. Varargs
def foo(values: String*){ } public void foo(String... values){ }
foo("bar", "baz") foo("bar", "baz");
val arr = Array("bar", "baz") String[] arr = new String[]{"bar",
foo(arr: _*) "baz"}
foo(arr);
16. (Almost) everything is an expression
val res = if (foo) x else y
val res = for (i <- 1 to 10) yield i // List(1, ..., 10)
val res = try { x } catch { ...; y } finally { } // x or y
17. Collections – List
Scala: Java:
val numbers = List(1, 2, 3) List<Integer> numbers =
new ArrayList<Integer>();
val numbers = 1 :: 2 :: 3 :: Nil numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers(0) numbers.get(0);
=> 1 => 1
18. Collections – Map
Scala: Java:
var m = Map(1 -> “apple”) Map<Int, String> m =
m += 2 -> “orange” new HashMap<Int, String>();
m.put(1, “apple”);
m.put(2, “orange”);
m(1) m.get(1);
=> “apple” => apple
23. Nice to know
Scala: Java:
Console.println(“Hello”) System.out.println(“Hello”);
println(“Hello”)
val line = Console.readLine() BufferedReader r = new BufferedReader(new
val line = readLine() InputStreamRead(System.in)
String line = r.readLine();
error(“Bad”) throw new RuntimeException(“Bad”)
1+1 new Integer(1).toInt() + new Integer(1).toInt();
1 .+(1)
1 == new Object new Integer(1).equals(new Object());
1 eq new Object new Integer(1) == new Object();
"""Asregex""".r java.util.regex.Pattern.compile(“Asregex”);