Scala · Basics

Introduction to Scala

Scala runs on the JVM, uses every Java library, and adds powerful functional programming with concise syntax.

Beginner 5 min Lesson 1 of 25Examples use Scala 3 (3.3 LTS or newer) with sbt or scala-cli.
0/25 in Scala
On this page · 5 min · Beginner

In simple words#

If Java is a reliable family car, Scala is the same engine (the JVM) in a sports body: shorter code, more features, and it still drives on every road Java does.

JVM
Same runtime as Java — Scala compiles to bytecode.
Functional programming
Building programs from pure functions and immutable data.
Scala 3
Current major version with cleaner syntax.

Learning objectives#

  • Explain what Scala is
  • Know where Scala is used
  • Write a Scala 3 main method
  • Compare Scala and Java briefly

Explanation#

Scala = “scalable language”: small scripts to large distributed systems.

It is fully object-oriented (every value is an object) and functional (functions are values; immutability is the default style).

Used in data engineering (Apache Spark, Kafka tooling), back ends (Play, http4s, ZIO, Akka/Pekko) and at companies like LinkedIn, Netflix and Twitter/X.

Scala 3 uses @main methods, optional braces with indentation, enums, given/using and extension methods.

Code example: Hello Scala 3#

ScalaHello.scala Illustrative
@main def hello(name: String = "Techie Babaji"): Unit =
  val greeting = s"Namaste from Scala, $name!"
  println(greeting)
  val squares = (1 to 5).map(n => n * n)
  println(s"Squares: ${squares.mkString(", ")}")


// Run:  scala run Hello.scala
Expected output
Namaste from Scala, Techie Babaji!
Squares: 1, 4, 9, 16, 25

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. @main marks the entry point.
  2. val declares an immutable value.
  3. s"…" interpolates variables.
  4. map transforms a range in one line.

Real-world use case#

Data engineers write Spark jobs in Scala to process terabytes of logs and transactions.

Common mistakes#

Writing Java-style Scala

Mutable vars and loops everywhere miss Scala’s strengths. Prefer val and collection methods.

Mixing Scala 2 and 3 tutorials

Syntax differs (implicit vs given, object Main extends App vs @main). Check the version.

Best practices#

  • Learn Scala 3 syntax
  • Prefer immutable values
  • Use the REPL to experiment
  • Learn Java basics first if new to the JVM

Remember this#

Scala = OOP + FP on the JVM.

@main starts a program.

val by default.

Practice exercise#

Your task: Print the sum of numbers from 1 to 100.

Show solution
Scala Illustrative
println((1 to 100).sum)   // 5050

Mini quiz#

Introduction to Scala quizMultiple choice Question 1 of 1

Which platform does Scala mainly run on?

Score so far: 0/0

Want more? Practise all Scala MCQs · Scala interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.