DEV Community

Rijul Rajesh
Rijul Rajesh

Posted on

Scala 101: Install, Code, and Build Your First Functions

If you're new to Scala, welcome! Scala is a powerful programming language that combines the best of both object-oriented and functional programming.

Why Learn Scala?

Scala has gained popularity for many reasons:

  • It's used in big data tools like Apache Spark.
  • It supports functional programming without sacrificing the benefits of OOP.
  • It's concise, expressive, and interoperable with Java.

Whether you're aiming for backend development or data engineering, Scala is a solid choice.

Step 1. Install Scala

sudo apt install scala
Enter fullscreen mode Exit fullscreen mode

Step 2: val vs var

In Scala, you define variables using either val or var.

val x = 10    // immutable (like final in Java)
var y = 5     // mutable
Enter fullscreen mode Exit fullscreen mode
  • Use val by default. It makes your code safer and more predictable.
  • Use var only if the value needs to change.

Step 3: Basic Types

Scala has a rich type system. Here are a few core types to start with:

val age: Int = 30
val pi: Double = 3.14
val name: String = "Alice"
val isScalaFun: Boolean = true
val nothingHere: Unit = ()  // Similar to void in Java
Enter fullscreen mode Exit fullscreen mode

Scala is statically typed, but it also has type inference—you can often skip explicit types:

val name = "Bob"  // Scala infers it's a String
Enter fullscreen mode Exit fullscreen mode

Step 4: Control Structures

Scala supports all the familiar flow-control constructs.

if Expression

val age = 20
val status = if (age >= 18) "Adult" else "Minor"
Enter fullscreen mode Exit fullscreen mode

match Expression

Scala’s powerful alternative to switch statements:

val day = "Monday"
val mood = day match {
  case "Monday" => "Sleepy"
  case "Friday" => "Excited"
  case _ => "Neutral"
}
Enter fullscreen mode Exit fullscreen mode

Loops

for (i <- 1 to 5) {
  println(i)
}

var count = 3
while (count > 0) {
  println(s"Countdown: $count")
  count -= 1
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Functions

Basic Syntax

def greet(name: String): String = {
  s"Hello, $name"
}
Enter fullscreen mode Exit fullscreen mode

Default & Named Parameters

def greet(name: String = "Stranger") = s"Hello, $name"

greet()         // Hello, Stranger
greet("Alice")  // Hello, Alice
Enter fullscreen mode Exit fullscreen mode

Step 6: Collections

Scala provides immutable collections by default. Let’s look at a few common ones:

val numbers = List(1, 2, 3)
val unique = Set(1, 2, 2, 3)
val phoneBook = Map("Alice" -> 1234, "Bob" -> 5678)
val arr = Array(10, 20, 30)
Enter fullscreen mode Exit fullscreen mode

You can iterate over them:

numbers.foreach(println)
Enter fullscreen mode Exit fullscreen mode

Or transform them:

val doubled = numbers.map(_ * 2)  // List(2, 4, 6)
Enter fullscreen mode Exit fullscreen mode

Step 7: Working with Option

Instead of null, Scala prefers using Option to handle missing values.

val maybeName: Option[String] = Some("Charlie")
val noName: Option[String] = None

def greet(nameOpt: Option[String]) = nameOpt match {
  case Some(name) => s"Hello, $name"
  case None => "Hello, guest"
}
Enter fullscreen mode Exit fullscreen mode

This pattern avoids NullPointerException and encourages safer code.

Wrapping up

Once you're comfortable with these basics, you're ready to dive into functional programming in Scala—higher-order functions, immutability, pattern matching, and more.

If you're a software developer who enjoys exploring different technologies and techniques like this one, check out LiveAPI. It’s a super-convenient tool that lets you generate interactive API docs instantly.

LiveAPI helps you discover, understand and use APIs in large tech infrastructures with ease!

So, if you’re working with a codebase that lacks documentation, just use LiveAPI to generate it and save time!

You can instantly try it out here! 🚀

Top comments (0)