0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Basic Syntax and Types

Understand Scala's fundamental syntax, variable declarations (val/var), common data types, and basic operators.

Basic Syntax and Types is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Scala's Clean Syntax

Welcome to Scala's basic syntax! Scala is designed to be concise and expressive, often allowing you to write less code to achieve more.

Unlike some languages, semicolons are usually optional at the end of a line if there's only one statement. This helps keep your code clean.

Expressions Are Key

In Scala, almost everything is an expression, meaning it evaluates to a value. This is a powerful concept that makes your code more functional and predictable.

Even a simple calculation or a conditional block can return a value.

Try running this example:

object Main {
  def main(args: Array[String]): Unit = {
    val x = 5 + 3
    println(s"The value of x is: $x")

    val message = if (x > 7) "Greater than 7" else "Not greater than 7"
    println(message)
  }
}

`val`: Immutable Values

When you declare a variable using val, you are creating an immutable value. This means once it's assigned, its value cannot be changed.

Immutability is a cornerstone of functional programming and helps prevent unexpected side effects, making your code safer and easier to reason about.

Try it out:

object Main {
  def main(args: Array[String]): Unit = {
    val greeting = "Hello, Coddy!"
    println(greeting)
    // greeting = "New message" // This would cause a compile error!
  }
}

`var`: Mutable Variables

Sometimes you need a variable whose value can change. For this, Scala provides var for mutable variables.

While useful, it's a good practice in Scala to favor val over var whenever possible to embrace immutability and functional style.

See how `var` works:

object Main {
  def main(args: Array[String]): Unit = {
    var counter = 0
    println(s"Initial counter: $counter")
    counter = counter + 1
    println(s"New counter: $counter")
  }
}

Numbers: Int, Long, Double

Scala supports common numeric types. The compiler often infers the type, but you can specify it.

  • Int: For whole numbers (e.g., 10)
  • Long: For larger whole numbers (suffix L, e.g., 10000000000L)
  • Double: For floating-point numbers (decimals, e.g., 3.14)

Here's an example:

object Main {
  def main(args: Array[String]): Unit = {
    val anInt = 42
    val aLong = 1234567890123L
    val aDouble = 3.14159

    println(s"Int: $anInt")
    println(s"Long: $aLong")
    println(s"Double: $aDouble")
  }
}

Boolean and Char Types

Beyond numbers, Scala has types for truth values and single characters:

  • Boolean: Represents truth values, either true or false.
  • Char: Stores a single character, enclosed in single quotes (e.g., 'A').

These are fundamental for logic and text processing.

Example:

object Main {
  def main(args: Array[String]): Unit = {
    val isActive = true
    val initial = 'C'

    println(s"Is active? $isActive")
    println(s"Initial character: $initial")
  }
}

Strings and Text

The String type is used for sequences of characters, or text. Strings are immutable in Scala, just like in Java.

A very useful feature is String Interpolation, which allows you to embed expressions directly within string literals using an s prefix and $ for variables.

Check this out:

object Main {
  def main(args: Array[String]): Unit = {
    val name = "Coddy"
    val age = 3

    val greeting = s"Hello, my name is $name and I am $age years old."
    println(greeting)

    val mathResult = s"Two plus two is ${2 + 2}"
    println(mathResult)
  }
}

Basic Arithmetic Operators

Scala provides standard arithmetic operators for performing calculations with numbers:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo - remainder of division)

These operators work as you'd expect, returning a new numeric value.

Let's do some math:

object Main {
  def main(args: Array[String]): Unit = {
    val a = 10
    val b = 3

    println(s"Addition: ${a + b}")
    println(s"Subtraction: ${a - b}")
    println(s"Multiplication: ${a * b}")
    println(s"Division: ${a / b} (integer division)")
    println(s"Modulo: ${a % b}")

    val c = 10.0
    val d = 3.0
    println(s"Float Division: ${c / d}")
  }
}

Comparison Operators

Comparison operators are used to compare two values and always return a Boolean (true or false).

  • == (Equal to)
  • != (Not equal to)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)

They are essential for control flow and decision-making.

object Main {
  def main(args: Array[String]): Unit = {
    val x = 10
    val y = 5

    println(s"x == y: ${x == y}")
    println(s"x != y: ${x != y}")
    println(s"x > y: ${x > y}")
    println(s"x <= y: ${x <= y}")
  }
}

Quick Check: Variables & Operators

Consider the following Scala code snippet:

object Main {
  def main(args: Array[String]): Unit = {
    val num1 = 7
    var num2 = 3
    num2 = num1 * 2
    val finalResult = num1 + num2 - 5
    println(finalResult)
  }
}

Recap: Syntax & Types

Great job! In this lesson, you've learned the fundamentals of Scala's basic syntax, including the use of expressions and the optional nature of semicolons.

We explored how to declare variables using val for immutable values and var for mutable ones. You also got familiar with common data types like Int, Long, Double, Boolean, Char, and String, including handy String Interpolation.

Finally, you practiced using basic arithmetic and comparison operators to perform calculations and logical checks. Keep practicing these basics as they are the building blocks for more complex Scala programs!

Frequently asked questions

Is the “Basic Syntax and Types” lesson free?

Yes — the full text of “Basic Syntax and Types” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Basic Syntax and Types”?

Understand Scala's fundamental syntax, variable declarations (val/var), common data types, and basic operators. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Basic Syntax and Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Getting Started with Scala
  2. Basic Syntax and Types
  3. Control Structures and Functions
← Back to Scala for Backend Engineering & Functional Programming