Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala vs Java 8 in a Java 8 World

1,065 views

Published on

Scala is becoming the language of choice for many development teams. This talk highlights how Scala excels in the world of multi-core processing and explores how it compares to Java 8.

Video Presentation: http://youtu.be/8vxTowBXJSg

Published in: Technology
  • Login to see the comments

Scala vs Java 8 in a Java 8 World

  1. 1. Scala in a Java 8 World
  2. 2. Takeaways •  Why Scala? •  Scala still relevant in Java 8 world?
  3. 3. What do we need in a language?
  4. 4. Developers should start thinking about tens, hundreds, and thousands of cores now “ ”- Intel
  5. 5. Parallel processing is hard
  6. 6. Mutable State public void setX(int x) { this.x = x; } setX(0) async { setX(getX()) + 1) } async { setX(getX()) * 2) }
  7. 7. Scala can help
  8. 8. Functional
  9. 9. Object Oriented
  10. 10. JVM Based
  11. 11. All logos owned by respective companies.
  12. 12. Isn’t Java 8 enough?
  13. 13. It’s all about developer productivity
  14. 14. class  Point  {          private  int  x;          private  int  y;              public  Point(int  x,  int  y)  {              setX(x);              setY(y);          }              public  int  getX()  {  return  x;  }          public  void  setX(int  x)  {  this.x  =  x;}          public  int  getY()  {  return  y;  }          public  void  setY(int  y)  {  this.y  =  y;}              public  boolean  equals(Object  other)  {              if  (other  instanceof  Point)  {                  Point  otherPoint  =  (Point)  other;                  return  otherPoint.getX()  ==  getX()  &&  otherPoint.getY()  ==  getY();              }  else  {                  return  false;              }          }              public  int  hashCode()  {              return  (new  Integer[]  {getX(),  getY()}).hashCode();          }   }
  15. 15. case class Point(var x: Int, var y: Int)
  16. 16. Traits abstract class Animal { def speak } trait FourLeggedAnimal { def walk def run } trait WaggingTail { val tailLength: Int def startTail { println("tail started") } def stopTail { println("tail stopped") } } class Dog extends Animal with WaggingTail with FourLeggedAnimal { def speak { println("Dog says 'woof'") } def walk { println("Dog is walking") } def run { println("Dog is running") } }
  17. 17. Immutable Classes public final class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } Java Scala case class Point(x: Int, y: Int)  
  18. 18. Immutable Data Structures Scala List(1, 2, 3) Set(1, 2, 3) Map(("foo", 1), ("bar", 2))   List<Integer> uList = Arrays.asList(1, 2, 3); Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); Set<Integer> uSet = Collections.unmodifiableSet(set); Map<String, Integer> map = new HashMap<>(); map.put("foo", 1); map.put("bar", 2); Map<String, Integer> uMap = Collections.unmodifiableMap(map); Java
  19. 19. Lambdas Scala peoples.filter(person => person.firstName == “Jon”)! ! peoples.stream().filter( person -> ! person.firstName.equals(”Jon”)).collect(Collectors.toList()) Java peoples.map(person => person.firstName) List(1, 2, 3).reduce(_+_) // 6 List(1, 2, 3).foreach(println)
  20. 20. public class Person { private String lastName; private String firstName; private String middleName; private String salutation; private String suffix; public Person(String lastName, String firstName, String middleName, String salutation, String suffix) { this.lastName = lastName; this.firstName = firstName; this.middleName = middleName; this.salutation = salutation; this.suffix = suffix; } }
  21. 21. public class Person { public static class PersonBuilder { public PersonBuilder lastName(String newLastName) { this.lastName = newLastName; return this; } public PersonBuilder firstName(String newFirstName) { this.firstName = newFirstName; return this; } public PersonBuilder middleName(String newMiddleName) { this.middleName = newMiddleName; return this; } public PersonBuilder salutation(String newSalutation) { this.salutation = newSalutation; return this; } public PersonBuilder suffix(String newSuffix) { this.suffix = newSuffix; return this; } public Person build() { return new Person(this); } } }
  22. 22. Named and Default Parameters case class Person(salutation: String = "Unknown", firstName: String = "Unknown”, middleName: String = "Unknown", lastName: String = "Unknown", suffix: String = "Unknown") Person("Mr", "John", "M”, "Doe”, ”Sr") Person(salutation = "Mr", firstName = "John", lastName = "Doe") Person(firstName = "John", middleName = "M", lastName = "Doe") Person(firstName = "John") Person(lastName = "Doe") Person(firstName = "John", lastName = "Doe")
  23. 23. Pattern Matching def  parseArgument(arg  :  String)
  24. 24. Pattern Matching def  parseArgument(arg  :  String)  =  arg  match  {   }  
  25. 25. Pattern Matching def  parseArgument(arg  :  String)  =  arg  match  {      case  ("-­‐h"  |  "-­‐-­‐help”)  =>  displayHelp()   }
  26. 26. Pattern Matching def  parseArgument(arg  :  String)  =  arg  match  {      case  ("-­‐h"  |  "-­‐-­‐help”)  =>  displayHelp()      case  bad  =>  badArgument(bad)   }
  27. 27. Pattern Matching def  parseArgument(arg  :  String,  value:  Any)
  28. 28. Pattern Matching def  parseArgument(arg  :  String,  value:  Any)  =  (arg,  value)  match  {   }  
  29. 29. Pattern Matching def  parseArgument(arg  :  String,  value:  Any)  =  (arg,  value)  match  {      case  ("-­‐h"  |  "-­‐-­‐help",  null)  =>  displayHelp()      case  ("-­‐l",  lang)  =>  setLanguageTo(lang)      case  bad  =>  badArgument(bad)   }
  30. 30. Pattern Matching def  parseArgument(arg  :  String,  value:  Any)  =  (arg,  value)  match  {      case  ("-­‐h"  |  "-­‐-­‐help",  null)  =>  displayHelp()      case  ("-­‐l",  lang)  =>  setLanguageTo(lang)      case  ("-­‐o"  |  "-­‐-­‐optim",  n  :  Int)  if  ((0  <  n)  &&  (n  <=  5))  =>  setOptimizationLevelTo(n)      case  ("-­‐o"  |  "-­‐-­‐optim",  badLevel)  =>  badOptimizationLevel(badLevel)      case  bad  =>  badArgument(bad)   }
  31. 31. Patterns // constant patterns case 0 => "zero” case true => "true” case "hello" => "you said 'hello'” case Nil => "an empty List" // sequence patterns case List(0, _, _) => "a three-element list with 0 as the first element” case List(1, _*) => "a list beginning with 1, having any number of elements” case Vector(1, _*) => "a vector starting with 1, having any number of elements” // constructor patterns case Person(first, "Alexander") => s"found an Alexander, first name = $first” case Dog("Suka") => "found a dog named Suka”
  32. 32. I call it my billion-dollar mistake. It was the invention of the null reference in 1965. “ ”- Tony Hoare
  33. 33. Option def toInt(in: String): Option[Int] = {     try {         Some(Integer.parseInt(in.trim))     } catch {         case ex: NumberFormatException => None     } } toInt(someString) match {     case Some(i) => println(i)     case None => println("That didn't work.") }
  34. 34. Collections with Option val bag = List("1", "2", "foo", "3", "bar") bag.map(toInt) // List[Option[Int]] = List(Some(1), Some(2), None, Some(3), None) bag.flatMap(toInt) // List(1, 2, 3)
  35. 35. Summary •  Parallel Processing made easier •  Developer Productivity •  Java interoperable •  Still relevant in a Java 8 world

×