Scala Enthusiasts BS Simon Barthel Scala for Java Programmers - - PowerPoint PPT Presentation

scala enthusiasts bs
SMART_READER_LITE
LIVE PREVIEW

Scala Enthusiasts BS Simon Barthel Scala for Java Programmers - - PowerPoint PPT Presentation

Scala Enthusiasts BS Simon Barthel Scala for Java Programmers Scala = scalable language 2 Martin Odersky Computer Scientist and Professor of programming methods Important Projects: Modula2, Pizza, Generic Java, current


slide-1
SLIDE 1

Enthusiasts BS Scala

Simon Barthel

Scala for Java Programmers

slide-2
SLIDE 2

Scala = „scalable language“

2

slide-3
SLIDE 3

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
slide-4
SLIDE 4

Scala

  • Combining worlds of OO and

functional paradigms

  • Strongly typed
  • Running in the JVM

4

slide-5
SLIDE 5

Yet another language?

5

New Frameworks

slide-6
SLIDE 6

Scala is a JVM Language

6

  • For the start just use your Java

experience!

  • E.g. use Maven-Scala-plugin

and 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
slide-7
SLIDE 7

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]
slide-8
SLIDE 8

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
slide-9
SLIDE 9

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
slide-10
SLIDE 10

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
slide-11
SLIDE 11

Hello World!

11

public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }

  • bject Main {

def main(args: Array[String]): Unit = { System.out.println("Hello World"); } }

slide-12
SLIDE 12

Classes

12

abstract class Person { public static int numArms = 2; public String name; public Person(String name) { this.name = name; } public abstract void eatBreakfest(); }

  • bject Person {

var numArms: Int = 2; } abstract class Person( var name: String ) { def eatBreakfest(): Unit; }

slide-13
SLIDE 13

Interfaces

13

interface AcademicPerson { public String getDegree(); } trait AcademicPerson { def getDegree(): String; }

  • Interfaces are now called traits
  • Apart from that the aforementioned rules apply
slide-14
SLIDE 14

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"; } }

slide-15
SLIDE 15

Inheritance

15

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

  • verride def eatBreakfest(): Unit = {

System.out.println("nomnomnom") }

  • verride def getDegree(): String = {

return "graduate"; } }

slide-16
SLIDE 16

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