Scala: Motivation

chapter, Online, 2016

Why Scala

If I were to pick a language to use today other than Java, it would be Scala.

James Gosling

Why Scala

Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a “replacement for Java” as Scala, and the momentum behind Scala is now unquestionable.

Charlies Olivier Nutter - JRuby lead



Java

public class Person {
  private int age;
  private String name;

  public Person(int age, String name) {
    this.age = age;
    this.name = name;
  }

  public int getAge() {
    return this.age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Scala

class Person(var age: Int, var name: String)

Java

List<Person> persons = ...
List<Person> adults = new LinkedList<Person>(); List<Person> kids = new LinkedList<Person>();
for (Person person : persons) {
  if (person.getAge() < 18) {
    kids.add(person);
  } else {
    adults.add(person);
  }
}

Scala

val (kids, adults) = persons.partition(_.age < 18)

Scala

  • Object oriented and functional
  • Statically typed
  • Java compatible
    • Complies to Java bytecode (and CLR)
    • Existing libraries/frameworks
  • Better Java


Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...