scala enthusiasts bsscala-bs.de/slides/meeting-1-scala-for-java-programmers.pdf · scala =...

16
Enthusiasts BS Scala Simon Barthel Scala for Java Programmers

Upload: others

Post on 12-Oct-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Enthusiasts BSScalaSimon Barthel

Scala for Java Programmers

Page 2: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Scala = „scalable language“

2

Page 3: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Martin Odersky

3

• Computer Scientist and Professor of programming methods

• Important Projects:• Modula2, Pizza, Generic Java, current

version of javac, and of course Scala

• 2001: Development of Scala

• 2004: First version

• 2011: Foundation of Typesafe

Page 4: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Scala

•Combining worlds of OO and functional paradigms

•Strongly typed

•Running in the JVM

4

Page 5: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Yet another language?

5

New Frameworks

Page 6: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Scala is a JVM Language

6

• For the start just use your Java experience!

• E.g. use Maven-Scala-pluginand start using Scala right now!

• Learn some awesome new Scala features when you have time

• Just switch back to Java when you have to be productive

• Smoothly learn Scala over time

Page 7: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Variables

int i = 5;

String s = "Hello World";

Collection<Double> l = new ArrayList<Double>();

7

var i: Int = 5;

var s: String = "Hello World";

var l: Collection[Double] = new ArrayList[Double]();

• Introduce new field/variable with var

• Type and identifier switch positions

• For generic types put type in [squared brackets]

Page 8: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Functions

public String firstNChars(String s, int n) {

return s.substring(0, n);

}

8

def firstNChars(s: String, n: Int): String = {

return s.substring(0, n);

}

• Introduce new method/function with def

• Type comes after the parameter list

• Add an ‘=‘ before the curly braces

Page 9: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

For-loop

for(int i=0; i<100; i++) {

System.out.println(i);

}

9

for(i <- 0 to 100) {

System.out.println(i);

}

• For-loops only iterate over iterable objects• Like Javas extended for-loop

• 0 to 100 creates a Range from 0 to 100

Page 10: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Try-Catch

try { ... }

catch(IOException ioe) { ... }

catch(SQLException se) { ... }

10

try { ... } catch {

case ioe: IOException => { ... }

case se: SQLException => { ... }

}

• Catch-Block is now a Partial Function• To be introduced later

Page 11: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Hello World!

11

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

}

}

object Main {

def main(args: Array[String]): Unit = {

System.out.println("Hello World");

}

}

Page 12: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Classes

12

abstract class Person {

public static int

numArms = 2;

public String name;

public Person(String name) {

this.name = name;

}

public abstract void

eatBreakfest();

}

object Person {

var numArms: Int = 2;

}

abstract class Person(var name: String

) {

def eatBreakfest(): Unit;

}

Page 13: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Interfaces

13

interface AcademicPerson {

public String getDegree();

}

trait AcademicPerson {

def getDegree(): String;

}

• Interfaces are now called traits

• Apart from that the aforementioned rules apply

Page 14: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Inheritance

14

class Bachelor extends Person implements AcademicPerson {

public Bachelor(String name) {

super(name);

}

@Override public void eatBreakfest() {

System.out.println("nomnomnom");

}

@Override public String getDegree() {

return "graduate";

}

}

Page 15: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Inheritance

15

class Bachelor(name: String) extends Person(name)with AcademicPerson {

override def eatBreakfest(): Unit = {

System.out.println("nomnomnom")

}

override def getDegree(): String = {

return "graduate";

}

}

Page 16: Scala Enthusiasts BSscala-bs.de/slides/Meeting-1-Scala-for-Java-Programmers.pdf · Scala = „scalable language“ ... Scala is a JVM Language 6 •For the start just use your Java

Scala-Maven-Plugin

•Start coding Scala in your current Java Project!• I show you how

•Look up instructions at:• https://github.com/scala-bs/meeting-1-MavenWithScalaAndJavaSources

16