Scala basics
;
Type definitions Scala s: String i: Int Java String s int i / Integer i
Variables Scala: val s = “Hello World” var i = 1 private var j = 3 Java: public final String s = “Hello World”; public int i = 1; private int j = 3;
Methods Scala: def add(x: Int, y: Int): Int = { x + y } def add(x: Int, y: Int) = x + y def doSomething(text: String) { } Java: public int add(int x, int y) { return x + y; } public void doSometing(String text) { }
Methods (2) Scala: myObject.myMethod(1) myObject myMethod(1) myObject myMethod 1 myObject.myOtherMethod(1, 2) myObject myOtherMethod(1, 2) myObject.myMutatingMethod() myObject.myMutatingMethod myObject myMutatingMethod Java: myObject.myMethod(1); myObject.myOtherMethod(1, 2); myObject.myMutatingMethod()
Methods (3) Scala: override def toString = ... Java: @Override public String toString() {...}
Classes and constructors Scala: class Person(val name: String) Java: public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
Traits (= Interface + Mixin) Scala: trait Shape { def area: Double } class Circle extends Object with Shape Java: interface Shape { public double area(); } public class Circle extends Object implements Shape
No “static” in Scala Scala: object PersonUtil { val AgeLimit = 18 def countPersons(persons: List[Person]) = ... } Java: public class PersonUtil { public static final int AGE_LIMIT = 18; public static int countPersons(List<Person>  persons) { ... } }
if-then-else Scala: if (foo) { ... } else if (bar) { ... } else { ... } Java: if (foo) { ... } else if (bar) { ... } else { ... }
For-loops Scala: for (i <- 0 to 3) { ... } for (s <- args) println(s) Java: for (int i = 0; i < 4; i++) { ... } for (String s : args) { System.out.println(s); }
While-loops Scala: while (true) { ... } Java: while (true) { ... }
Exceptions Scala: throw new Exception(“...”) try { } catch { case e: IOException => ... } finally { } Java: throw new Exception(“...”) try { } catch (IOException e) { ... } finally { }
Varargs def foo(values: String*){ } foo(&quot;bar&quot;, &quot;baz&quot;) val arr = Array(&quot;bar&quot;, &quot;baz&quot;) foo(arr: _*) public void foo(String... values){ } foo(&quot;bar&quot;, &quot;baz&quot;); String[] arr = new String[]{&quot;bar&quot;, &quot;baz&quot;} foo(arr);
(Almost) everything is an expression val res = if (foo) x else y val res = for (i <- 1 to 10) yield i  // List(1, ..., 10) val res = try { x } catch { ...; y } finally { }  // x eller y
Collections – List Scala: val numbers = List(1, 2, 3) val numbers = 1 :: 2 :: 3 :: Nil numbers(0) => 1 Java: List<Integer> numbers =  new ArrayList<Integer>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.get(0); => 1
Collections – Map Scala: var m = Map(1 -> “apple”) m += 2 -> “orange” m(1) => “apple” Java: Map<Int, String> m =  new HashMap<Int, String>(); m.put(1, “apple”); m.put(2, “orange”); m.get(1); => apple
Generics Scala: List[String] Java: List<String>
Tuples Scala: val tuple: Tuple2[Int, String] =  (1, “apple”) val quadruple =  (2, “orange”, 0.5d, false) Java: Pair<Integer, String> tuple =  new Pair<Integer, String>(1, “apple”) ... yeah right... ;-)
Packages Scala: package mypackage ... Java: package mypackage; ...
Imports Scala: import java.util.{List, ArrayList} import java.io._ import java.sql.{Date => SDate} Java: import java.util.List import java.util.ArrayList import java.io.* ???
Nice to know Scala: Console.println(“Hello”) println(“Hello”) val line = Console.readLine() val line = readLine() error(“Bad”) 1 + 1 1 .+(1) 1 == new Object 1 eq new Object &quot;&quot;&quot;A\sregex&quot;&quot;&quot;.r Java: System.out.println(“Hello”); BufferedReader r = new BufferedReader(new InputStreamRead(System.in) String line = r.readLine(); throw new RuntimException(“Bad”) new Integer(1).toInt() + new  Integer(1).toInt(); new Integer(1).equals(new Object()); new Integer(1) == new Object(); java.util.regex.Pattern.compile(“A\\sregex”);

More Related Content

ODP
1.2 scala basics
PDF
Swift rocks! #1
PDF
Swift Rocks #2: Going functional
ODP
2.1 Recap From Day One
PDF
A bit about Scala
PDF
Scala for Jedi
PDF
First-Class Patterns
PDF
Scala vs Java 8 in a Java 8 World
1.2 scala basics
Swift rocks! #1
Swift Rocks #2: Going functional
2.1 Recap From Day One
A bit about Scala
Scala for Jedi
First-Class Patterns
Scala vs Java 8 in a Java 8 World

What's hot (19)

PDF
Introduction to Scala
ODP
JavaScript Web Development
PPTX
Scala Back to Basics: Type Classes
PDF
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
PDF
Java 8 - An Introduction by Jason Swartz
PDF
Scala introduction
PDF
Procedural Programming: It’s Back? It Never Went Away
PDF
Lambda? You Keep Using that Letter
PPT
Scala presentation by Aleksandar Prokopec
PPT
Collection v3
PPTX
ES6 and AngularAMD
PDF
Futures e abstração - QCon São Paulo 2015
PDF
Scala vs java 8
PDF
Refactoring to Immutability
PDF
High Wizardry in the Land of Scala
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
PDF
Demystifying functional programming with Scala
PPTX
Yin Yangs of Software Development
PPT
Collection Core Concept
Introduction to Scala
JavaScript Web Development
Scala Back to Basics: Type Classes
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Java 8 - An Introduction by Jason Swartz
Scala introduction
Procedural Programming: It’s Back? It Never Went Away
Lambda? You Keep Using that Letter
Scala presentation by Aleksandar Prokopec
Collection v3
ES6 and AngularAMD
Futures e abstração - QCon São Paulo 2015
Scala vs java 8
Refactoring to Immutability
High Wizardry in the Land of Scala
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Demystifying functional programming with Scala
Yin Yangs of Software Development
Collection Core Concept
Ad

Similar to 1.2 Scala Basics (20)

ODP
1.2 scala basics
ODP
Introduction to Scala
PDF
Workshop Scala
ODP
Scala introduction
PDF
Introduction to Scala
ODP
2.1 recap from-day_one
ODP
Introducing scala
PPT
Scala introduction
PDF
Scala - en bedre og mere effektiv Java?
PDF
Scala Bootcamp 1
PPT
Rewriting Java In Scala
PDF
Java Cheat Sheet
PDF
Scala - en bedre Java?
PPTX
PDF
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
PPT
JBUG 11 - Scala For Java Programmers
PDF
(How) can we benefit from adopting scala?
PPT
Scala uma poderosa linguagem para a jvm
PDF
The Scala Programming Language
PDF
Scala in Places API
1.2 scala basics
Introduction to Scala
Workshop Scala
Scala introduction
Introduction to Scala
2.1 recap from-day_one
Introducing scala
Scala introduction
Scala - en bedre og mere effektiv Java?
Scala Bootcamp 1
Rewriting Java In Scala
Java Cheat Sheet
Scala - en bedre Java?
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JBUG 11 - Scala For Java Programmers
(How) can we benefit from adopting scala?
Scala uma poderosa linguagem para a jvm
The Scala Programming Language
Scala in Places API
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
CloudStack 4.21: First Look Webinar slides
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Abstractive summarization using multilingual text-to-text transfer transforme...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Architecture types and enterprise applications.pdf
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
Configure Apache Mutual Authentication
PPTX
Modernising the Digital Integration Hub
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Enhancing emotion recognition model for a student engagement use case through...
NewMind AI Weekly Chronicles – August ’25 Week III
A comparative study of natural language inference in Swahili using monolingua...
Custom Battery Pack Design Considerations for Performance and Safety
Zenith AI: Advanced Artificial Intelligence
Developing a website for English-speaking practice to English as a foreign la...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
CloudStack 4.21: First Look Webinar slides
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
A proposed approach for plagiarism detection in Myanmar Unicode text
Getting started with AI Agents and Multi-Agent Systems
Abstractive summarization using multilingual text-to-text transfer transforme...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Architecture types and enterprise applications.pdf
A contest of sentiment analysis: k-nearest neighbor versus neural network
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Configure Apache Mutual Authentication
Modernising the Digital Integration Hub
Chapter 5: Probability Theory and Statistics
Enhancing emotion recognition model for a student engagement use case through...

1.2 Scala Basics

  • 2. ;
  • 3. Type definitions Scala s: String i: Int Java String s int i / Integer i
  • 4. Variables Scala: val s = “Hello World” var i = 1 private var j = 3 Java: public final String s = “Hello World”; public int i = 1; private int j = 3;
  • 5. Methods Scala: def add(x: Int, y: Int): Int = { x + y } def add(x: Int, y: Int) = x + y def doSomething(text: String) { } Java: public int add(int x, int y) { return x + y; } public void doSometing(String text) { }
  • 6. Methods (2) Scala: myObject.myMethod(1) myObject myMethod(1) myObject myMethod 1 myObject.myOtherMethod(1, 2) myObject myOtherMethod(1, 2) myObject.myMutatingMethod() myObject.myMutatingMethod myObject myMutatingMethod Java: myObject.myMethod(1); myObject.myOtherMethod(1, 2); myObject.myMutatingMethod()
  • 7. Methods (3) Scala: override def toString = ... Java: @Override public String toString() {...}
  • 8. Classes and constructors Scala: class Person(val name: String) Java: public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
  • 9. Traits (= Interface + Mixin) Scala: trait Shape { def area: Double } class Circle extends Object with Shape Java: interface Shape { public double area(); } public class Circle extends Object implements Shape
  • 10. No “static” in Scala Scala: object PersonUtil { val AgeLimit = 18 def countPersons(persons: List[Person]) = ... } Java: public class PersonUtil { public static final int AGE_LIMIT = 18; public static int countPersons(List<Person> persons) { ... } }
  • 11. if-then-else Scala: if (foo) { ... } else if (bar) { ... } else { ... } Java: if (foo) { ... } else if (bar) { ... } else { ... }
  • 12. For-loops Scala: for (i <- 0 to 3) { ... } for (s <- args) println(s) Java: for (int i = 0; i < 4; i++) { ... } for (String s : args) { System.out.println(s); }
  • 13. While-loops Scala: while (true) { ... } Java: while (true) { ... }
  • 14. Exceptions Scala: throw new Exception(“...”) try { } catch { case e: IOException => ... } finally { } Java: throw new Exception(“...”) try { } catch (IOException e) { ... } finally { }
  • 15. Varargs def foo(values: String*){ } foo(&quot;bar&quot;, &quot;baz&quot;) val arr = Array(&quot;bar&quot;, &quot;baz&quot;) foo(arr: _*) public void foo(String... values){ } foo(&quot;bar&quot;, &quot;baz&quot;); String[] arr = new String[]{&quot;bar&quot;, &quot;baz&quot;} foo(arr);
  • 16. (Almost) everything is an expression val res = if (foo) x else y val res = for (i <- 1 to 10) yield i // List(1, ..., 10) val res = try { x } catch { ...; y } finally { } // x eller y
  • 17. Collections – List Scala: val numbers = List(1, 2, 3) val numbers = 1 :: 2 :: 3 :: Nil numbers(0) => 1 Java: List<Integer> numbers = new ArrayList<Integer>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.get(0); => 1
  • 18. Collections – Map Scala: var m = Map(1 -> “apple”) m += 2 -> “orange” m(1) => “apple” Java: Map<Int, String> m = new HashMap<Int, String>(); m.put(1, “apple”); m.put(2, “orange”); m.get(1); => apple
  • 19. Generics Scala: List[String] Java: List<String>
  • 20. Tuples Scala: val tuple: Tuple2[Int, String] = (1, “apple”) val quadruple = (2, “orange”, 0.5d, false) Java: Pair<Integer, String> tuple = new Pair<Integer, String>(1, “apple”) ... yeah right... ;-)
  • 21. Packages Scala: package mypackage ... Java: package mypackage; ...
  • 22. Imports Scala: import java.util.{List, ArrayList} import java.io._ import java.sql.{Date => SDate} Java: import java.util.List import java.util.ArrayList import java.io.* ???
  • 23. Nice to know Scala: Console.println(“Hello”) println(“Hello”) val line = Console.readLine() val line = readLine() error(“Bad”) 1 + 1 1 .+(1) 1 == new Object 1 eq new Object &quot;&quot;&quot;A\sregex&quot;&quot;&quot;.r Java: System.out.println(“Hello”); BufferedReader r = new BufferedReader(new InputStreamRead(System.in) String line = r.readLine(); throw new RuntimException(“Bad”) new Integer(1).toInt() + new Integer(1).toInt(); new Integer(1).equals(new Object()); new Integer(1) == new Object(); java.util.regex.Pattern.compile(“A\\sregex”);