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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.