Scala

Object-oriented programming (OOP)

All types are classes.

All data are objects.

All computation are method invocations.

Java as an OOP

Type as class:

class String {
    String substring(int start, int end) { ... }
}

Data as object:

String message = "Hello world";

Computations as method invocations:

System.out.println(message.substring(3, 6));

Java as non-OOP

  1. Types are not classes:

    • int, float, etc. are not classes.

    • Try: List<int> numbers;

  2. Data are not objects:

    • "Hello world".hashCode() works because "Hello world" is an object, and all objects are with the method int hashCode().

    • 123.hashCode() fails.

  3. Computation is not method invocation:

    i = 1 + 2
    

    The integer addition + is not a method.

Scala

List[Int]
Array[Float]

! Parameteric types

print(1.toBinaryString)

! Invoking a method of integer that accepts no arguments.

1 + 2
1.+(2)

! Addition + is a method of an integer 1.

_____

Hello world

Setting up the compiler:

  1. Download scala-2.xx.x.tgz from the download site: http://www.scala-lang.org/download/

  2. Uncompress the file, and you will find bin/scala which is the interpreter for Scala.

REPL

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.

scala> println("Hello world")

Scripting

  1. Create a file hello.scala
  2. Run it scala hello.scala
back
© KEN PU, 2016