0% found this document useful (0 votes)
6 views5 pages

SCALA Basics

Uploaded by

Praveena G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

SCALA Basics

Uploaded by

Praveena G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SCALA Basics

Chapter 1: Introduction to Scala

Scala is a modern programming language that seamlessly blends object-oriented and functional
programming paradigms. Running on the JVM (Java Virtual Machine), it interacts smoothly with Java
libraries and frameworks. Designed for brevity and expressiveness, Scala empowers developers with
powerful abstractions while maintaining elegant syntax.

Key Features:

 Interoperability with Java: Easily integrates with existing Java libraries and frameworks.

 Concise Syntax: Minimizes boilerplate code compared to Java.

 Functional Programming: Embraces immutability, higher-order functions, and pattern


matching.

 Concurrency: Utilizes Akka for efficient reactive and parallel programming.

 Strong Static Typing: Detects errors at compile time to ensure robust, reliable code.

Chapter 2: Scala vs. Java

Scala enhances Java by adopting modern paradigms and offering advanced features that simplify
development:

Feature Java Scala

Syntax Verbose and line-intensive Concise and expressive

Type Inference Requires explicit declarations Automatically inferred types

Concurrency Uses Threads & Executors Leverages Akka for reactive programming

Functional Programming Limited native support Fully integrated functional approach

Pattern Matching Requires manual logic Built directly into the language

Scala is especially beneficial for projects involving big data processing, web applications, and
distributed systems.

Chapter 3: Variables and Data Types

Scala distinguishes between two types of variables:

 Immutable (val): Once defined, the value cannot change.

 Mutable (var): The value can be updated 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 provides several control structures for managing the flow of your program:

 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

In Scala, functions are first-class citizens. This means you can store functions in variables, pass them
as parameters, or return them from other functions.

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 boasts a robust collection framework that includes:

 Lists: Ordered sequences of elements.

 Sets: Collections of unique elements.

 Maps: Collections of 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 fully supports object-oriented programming 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 provides a powerful alternative to traditional switch-case logic, making it
easier to work with complex data structures.

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 parallel and distributed
application development.

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 a popular choice for web development, particularly with frameworks like Play Framework
that emphasize reactive and scalable architectures.

Example:

scala

CopyEdit

import play.api._

import play.api.mvc._

object HelloController extends Controller {

def hello = Action { Ok("Hello, Scala Web!") }

You might also like