SlideShare a Scribd company logo
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;Aregex&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(“Asregex”);

More Related Content

What's hot

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Raúl Raja Martínez
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Development
vito jeng
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Ruslan Shevchenko
 
Java 8 - An Introduction by Jason Swartz
Java 8 - An Introduction by Jason SwartzJava 8 - An Introduction by Jason Swartz
Java 8 - An Introduction by Jason Swartz
Jason Swartz
 
Scala introduction
Scala introductionScala introduction
Scala introduction
vito jeng
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
Kevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
Kevlin Henney
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Collection v3
Collection v3Collection v3
Collection v3
Sunil OS
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
dhaval10690
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
François Sarradin
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
Kevlin Henney
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
Denis
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
Naveenkumar Muguda
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
Rays Technologies
 

What's hot (19)

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

Similar to 1.2 Scala Basics

1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
wpgreenway
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Lorenzo Dematté
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
Bert Van Vreckem
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Alf Kristian Støyle
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Aleksandar Prokopec
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
futurespective
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
Meetu Maltiar
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Yardena Meymann
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
Knoldus Inc.
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
Saeid Zebardast
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
Jesper Kamstrup Linnet
 
Scala
ScalaScala
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done RightJDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
PROIDEA
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
Isaias Barroso
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 

Similar to 1.2 Scala Basics (20)

1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Scala
ScalaScala
Scala
 
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done RightJDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 

Recently uploaded

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 

Recently uploaded (20)

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 

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;Aregex&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(“Asregex”);