Getting Started with Scala
Welcome to Scala FYI! This guide will help you get started with Scala programming, from installation to writing your first programs.
What is Scala?
Scala is a modern, multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It seamlessly integrates features of object-oriented and functional programming languages.
Key Features
- Concise: Scala reduces boilerplate code significantly
- Type-safe: Strong static typing prevents many runtime errors
- Functional: First-class functions and immutable data structures
- Object-oriented: Every value is an object, every operation is a method call
- JVM compatibility: Runs on the Java Virtual Machine with full Java interoperability
Installation
Prerequisites
Before installing Scala, make sure you have:
- Java 8 or later (JDK 11+ recommended)
- A terminal or command prompt
Installing Scala
Option 1: Using SDKMAN (Recommended)
# Install SDKMAN
curl -s "https://get.sdkman.io" | bash
# Install latest Scala version
sdk install scala
# Install sbt (Scala Build Tool)
sdk install sbt
Option 2: Using Homebrew (macOS)
brew install scala
brew install sbt
Option 3: Manual Installation
- Download Scala from scala-lang.org
- Extract the archive
- Add the
bin
directory to your PATH
Your First Scala Program
Hello World
Create a file called HelloWorld.scala
:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, Scala FYI!")
}
}
Compile and run:
scalac HelloWorld.scala
scala HelloWorld
Using the REPL
Scala includes an interactive REPL (Read-Eval-Print Loop):
scala
Try some basic expressions:
scala> val name = "Scala FYI"
scala> println(s"Welcome to $name!")
scala> val numbers = List(1, 2, 3, 4, 5)
scala> numbers.map(_ * 2)
Basic Concepts
Values and Variables
val immutable = "Can't change this" // Immutable
var mutable = "Can change this" // Mutable
Functions
def greet(name: String): String = {
s"Hello, $name!"
}
// Shorter syntax for simple functions
def square(x: Int): Int = x * x
Collections
// List (immutable)
val fruits = List("apple", "banana", "cherry")
// Array (mutable)
val numbers = Array(1, 2, 3, 4, 5)
// Map
val capitals = Map(
"France" -> "Paris",
"Spain" -> "Madrid"
)
Next Steps
Now that you have Scala set up, explore these topics:
- Object-Oriented Programming - Classes, objects, and inheritance
- Functional Programming - Higher-order functions and immutability
- Collections - Working with Scala’s collection library
- Pattern Matching - Powerful control structures
- Build Tools - sbt, Mill, and project management
Resources
Ready to dive deeper? Check out our comprehensive documentation for detailed guides and examples.