High-level Overview
- Scala is a high-level, general-purpose programming language with support for both object-oriented and functional programming
- Scala is statically typed and compiled into Java bytecode (JVM instructions)
- All types and functions are objects (instances)
- It provides seamless Java interoperability — all classed from the java.lang the package is imported by default
- Designed for concurrent, distributed, and message-driven applications – strong static systems, data science, and machine learning
- It has nested functions support
- Runs on JVM as well as JavaScript (Scala.js)
- SBT is a popular open-source build tool for Scala projects
- IntelliJ is a good Scala IDE
- Official Scala Lang –> tutorials
Scala vs. Java – Key Differences
Scala | Java | |
Concurrency | Actor model | Thread-based model |
Variables | Immutable by default | Mutable by default |
Operator Overloading | Yes | No |
Lazy Evaluation | Yes | No |
Pattern Matching & Big Math | Yes | No |
Build Process | Slow | Faster |
Scala Syntax
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
- Singleton Object model – a class with a single instance
- Unit is used to define a function that does not return anything, similar to void in Java
Variables & Types
// Declaring immutable variables
// val <Name>: <Type> = <Literal>
val a: Int = 5
val b: Long = 100000000L
val c: Short = 1
val d: Double = 2.50
val e: Float = 2.50f
val f: String = "allaboutscala Donut Store"
val g: Byte = 0xa
val h: Char = 'D'
val i: Unit = ()
// Declare a variable with no initialization
var j: String = _
// Declaring mutable variables
// var <Name> : <Type> = <Literal>
var x: Int = 1
var y: String = "text"
var z: Double = _
String Interpolation
val x: String = "some text"
println(s"my variable text is $favoriteDonut")
Scala Frameworks
Big Data | Apache Spark – leading open sourced platform for large scale data processing |
High Throughput Messaging | Kafka – high throughput distributed messaging system |
Highly Concurrent Systems | Akka – build highly concurrent and distributed systems Finagle – highly concurrent servers with protocol agnostic APIs Spray – high throughput HTTP server |
Other Frameworks | Play – scalable web applications Shapeless – added functionality to data types ScalaTest – testing framework Scalaz – additional semantics for functional programming Slick – rich data access layer |