Scala for Java developers

Lesson 1: Getting started and familiarize

Introductions

  • Welcome!
  • Introductions
    • name
    • familiar with Java
    • any knowledge of Scala
    • why Scala

Pre-setup from home

Clone project

https://github.com/soniarodriguez/scala-for-java-devs

 

Open project in IntelliJ

 

Install Scala plugin

Introduction to Scala

What is Scala?

  • Martin Odersky
    • Scala (2001)
    • javac (1998-1999)
    • Java Generics (1998)

What is Scala?

  • Scalable programming language

  • Built in a multi-paradigm way

    • Object-oriented programming

      • Data abstraction with objects

    • Functional programming

      • Mathematical functions, no side effects

Introduction to SBT

What is SBT?

  • Interactive build tool

  • Useful commands
sbt test
sbt run
sbt clean
sbt console

What is SBT?

  • Starting point of a project:

    • build.sbt 

Basic Scala's knowledge

Scala vs Java

  • Scala also runs on the JVM

  • Easy to interact with Java code

    • Some Java imports by default

Scala vs Java syntax

  • No need of semicolons at the end of line
  • No need to specify the type when defining a variable
val result1 = 5 + 2
val result2 = 3 + 1 
  • Method definition, no need of return
<scope> def <functionName>(<parameterName: ParameterType>*): <returnType> = {
	body
}
def canFly(animal: String): Boolean = {
    animal.equalsIgnoreCase("bird")
}

Example

Scala vs Java syntax

import scala.collection.JavaConverters._
def getFirstNPrimeNumers(n: Int): List[Int]
  • Multiple imports with Scala use underscore _ instead of asterisk *
  • Type parameters: [] instead of <>

Scala as Pure Object Oriented Programming Language

  • Bullet One
  • Bullet Two
  • Bullet Three

Scala as Pure OOP Language

  • Everything is an OBJECT
    • Even primitive types and functions

​        Example:

  • Operations with Java primitive types
scala> val result = 1 + 4 * 2
result: Int = 9
scala> val result = 1.+(4.(*2))
result: Int = 9

Using infix syntax

Calling methods of a number

Scala as Pure OOP Language

  • Possible operations for a Integer
sbt console

Using tab to autocomplete

Scala for Java developers. Lesson 1

By Sonia Fernández Rodríguez

Scala for Java developers. Lesson 1

  • 72