Scala as Concise Java

Java code often has a lot of boilerplate code—getters, setters, access modifiers, code to deal with checked exceptions…. The growing list bloats the code. As you’ll see here, the Scala compiler walks a few extra miles so you don’t have to expend efforts to write and maintain code that can be generated.

Less Boilerplate

Scala has very high code density—you type less to achieve more. To contrast, let’s start with an example of Java code:

FromJavaToScala/Greetings.java
 
//Java code
 
public​ ​class​ Greetings {
 
public​ ​static​ ​void​ main(​String​​[]​ args) {
 
for​(​int​ i = 1; i < 4; i++) {
 
System​.out.print(i + ​","​);
 
}
 
System​.out.println(​"Scala Rocks!!!"​);
 
}
 
}

Here’s the output:

 
1,2,3,Scala Rocks!!! ...

Get Pragmatic Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.