O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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!!! ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required