HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ScalaVariables (val / var)Data TypesStringsListsConditions (if / match)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Scala›Introduction to Scala

Introduction to Scala

First Scala program: entry point with @main and printing to the screen with println.

Simple enough for a 5-year-old to understand

Your first program

If you mark a function with @main in Scala 3, the program starts from there. println prints the text to the screen.

Hello
1
@main def run() =
The entry point of the program.
FunctionThe entry point of the program.
2
println("Hello!")
Prints the text inside the quotes.
Output / InputPrints the text inside the quotes.
FunctionOutput / Input
💡 Hint: In Scala 3, you can use indentation instead of curly braces.

Multiple lines

Example code
1
@main def run() =
2
println("First line")
Prints the first line.
Output / InputPrints the first line.
3
println("Second line")
Moves to the next line and prints the second one.
Output / InputMoves to the next line and prints the second one.
Output / Input
💡 Hint: Lines in the same block must have the same amount of indentation.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.Which annotation makes a function the program entry point in Scala 3?
2.What is the difference between println and print?

About this topic

Scala is a language that runs on the Java Virtual Machine and combines object-oriented programming with functional programming into a single language. It was designed in 2004 by Martin Odersky as a response to Java's verbosity and limited expressive power.

Scala's best-known use case is big data processing. Apache Spark is written in Scala, which is the main reason for the language's popularity in data engineering. Aside from that, it is also preferred for high-traffic backend services.

The language allows you to write the same task in multiple ways: you can write object-oriented code similar to Java, or functional code close to Haskell. This flexibility is a strong advantage, but if a common coding style is not established within the team, it reduces readability.

What you will learn

  • The entry point of a Scala program
  • printing to the screen with println
  • that semicolons are not required
  • Writing comment lines
  • The beginning of the object and class distinction

Who is this for?

  • Those who know Java and want to transition to functional programming
  • Engineers who will process data with Spark
  • Those looking for a highly expressive language on the JVM

When to use?

Scala is powerful in systems that require big data pipelines, concurrent backend services, and complex domain models.

It feels heavy for small scripts and quick prototypes; compilation times are long, and the learning curve of the language is steep.

Common mistakes

Wrong

public static void main(String[] args) {
  System.out.println("Hello")
}

This is Java syntax and does not compile in Scala. In Scala, the entry point is defined inside an object or, in newer versions, marked directly, and a short function is enough to print to the screen. Those coming from Java write this pattern out of reflex.

Correct

object Main {
  def main(args: Array[String]): Unit = {
    println("Hello")
  }
}

Wrong

object Main {
  def main(args: Array[String]): Unit = {
    val ad = "Ayşe"
    println("Merhaba $ad")
  }
}

String interpolation does not work automatically in Scala; you need to put an s in front of the text. Otherwise, the variable name is printed to the screen literally along with the dollar sign. Those coming from Kotlin or JavaScript constantly forget this letter.

Correct

object Main {
  def main(args: Array[String]): Unit = {
    val ad = "Ayşe"
    println(s"Merhaba $ad")
  }
}

Wrong

object Main {
  def main(args: Array[String]) {
    println("Hello")
  }
}

A body written without an equals sign is the old procedural syntax; the function returns Unit in any case, and this syntax has been removed in newer versions of Scala. Writing an equals sign before the body is now mandatory and prevents errors early on in functions with a return value.

Correct

object Main {
  def main(args: Array[String]): Unit = {
    println("Hello")
  }
}

Frequently asked questions

What is the difference between Scala 2 and Scala 3?

Scala 3 simplified the syntax: it introduced indentation-based block writing, a simpler entry point definition, and a revamped implicit values system. The code of both versions is mostly compatible, but some advanced features have changed; when looking at examples, you need to pay attention to which version they belong to.

Can I use Java libraries?

Yes, Scala is fully compatible with Java. You can call existing Java classes directly and use both languages together in the same project. This compatibility allowed Scala to quickly find a place in the Java ecosystem.

Should I use semicolons?

Not needed; the newline ends the statement. It is only required as a separator if you write multiple statements on the same line, which is not a recommended style anyway. Semicolons written out of habit from Java create unnecessary clutter.

Summary

The entry point is defined inside an object.

Precede the text with an s for string interpolation.

The equals sign before the body is mandatory.

Semicolons are not required.

Scala is fully compatible with Java.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code