SCALA Basics
Chapter 1: Introduction to Scala
Scala is a modern programming language that artfully combines object-oriented and functional
programming paradigms. Running on the JVM (Java Virtual Machine), it ensures smooth
interoperability with Java libraries and frameworks. Scala’s design emphasizes brevity, elegance, and
expressiveness, making it a powerful tool for developers.
Key Features:
Interoperability with Java: Seamlessly works with Java libraries and frameworks.
Concise Syntax: Minimizes boilerplate compared to Java.
Functional Programming: Supports immutability, higher-order functions, and pattern
matching.
Concurrency: Leverages Akka for efficient reactive and parallel programming.
Strong Static Typing: Catches errors at compile time for more robust code.
Chapter 2: Scala vs. Java
Scala enhances many aspects of Java by embracing modern paradigms and offering advanced
features:
Feature Java Scala
Syntax Verbose; requires many lines Concise; expressive and succinct
Type Inference Requires explicit type declarations Automatically inferred types
Concurrency Managed via Threads & Executors Uses Akka for reactive programming
Functional Programming Limited native support Fully integrated functional approach
Pattern Matching Requires manual logic Built into the language
Scala is particularly advantageous for big data processing, web applications, and distributed systems.
Chapter 3: Variables and Data Types
Scala distinguishes between two variable types:
Immutable (val): Once assigned, their value cannot be changed.
Mutable (var): Their value can be modified after initialization.
Example:
scala
CopyEdit
val immutableVar = 10 // Immutable: cannot be reassigned
var mutableVar = 20 // Mutable: value can change
mutableVar = 30 // New value assigned
Common Data Types:
Int: 32-bit integer (e.g., 10, -3)
Double: Floating-point numbers (e.g., 3.14)
Boolean: true or false
String: A sequence of characters (e.g., "Hello")
Chapter 4: Control Flow and Loops
Scala offers a variety of control structures:
Conditional Statements: if-else
Looping Constructs: for and while loops
Conditional Example:
scala
CopyEdit
val x = 15
if (x > 10) println("Greater than 10")
else println("Less or equal to 10")
Looping Example:
scala
CopyEdit
for (i <- 1 to 5) println(i) // Outputs numbers 1 through 5
Chapter 5: Functions in Scala
Functions in Scala are first-class citizens—they can be stored in variables, passed as arguments, or
returned as values.
Defining a Function:
scala
CopyEdit
def add(x: Int, y: Int): Int = x + y
println(add(5, 3)) // Outputs: 8
Higher-Order Function Example:
scala
CopyEdit
val multiply = (x: Int, y: Int) => x * y
println(multiply(3, 4)) // Outputs: 12
Chapter 6: Collections and Functional Programming
Scala’s rich collection libraries include:
Lists: Ordered sequences.
Sets: Collections of unique elements.
Maps: Key-value pairs.
Example:
scala
CopyEdit
val numbers = List(1, 2, 3, 4, 5)
val squaredNumbers = numbers.map(x => x * x)
println(squaredNumbers) // Outputs: List(1, 4, 9, 16, 25)
Chapter 7: Object-Oriented Programming in Scala
Scala supports core OOP principles such as classes, objects, inheritance, and polymorphism.
Example:
scala
CopyEdit
class Person(val name: String, val age: Int) {
def greet(): String = s"Hello, my name is $name and I am $age years old."
val person = new Person("Alice", 25)
println(person.greet())
Chapter 8: Pattern Matching & Case Classes
Pattern matching in Scala is an advanced alternative to traditional switch-case constructs.
Example:
scala
CopyEdit
def matchTest(x: Int): String = x match {
case 1 => "One"
case 2 => "Two"
case _ => "Other"
println(matchTest(2)) // Outputs: Two
Chapter 9: Concurrency with Akka
Scala leverages Akka—an actor-based concurrency model that simplifies writing parallel applications.
Example:
scala
CopyEdit
import akka.actor._
class HelloActor extends Actor {
def receive = {
case "hello" => println("Hello, Akka!")
case _ => println("Unknown message")
val system = ActorSystem("HelloSystem")
val helloActor = system.actorOf(Props[HelloActor], name = "helloactor")
helloActor ! "hello"
Chapter 10: Scala for Web Development
Scala is popular in web development, particularly with frameworks like Play Framework, known for
its reactive and scalable nature.
Example:
scala
CopyEdit
import play.api._
import play.api.mvc._
object HelloController extends Controller {
def hello = Action { Ok("Hello, Scala Web!") }