Values in Scala

Our first example will demonstrate various value types in Scala, including strings, integers, floats, and booleans. Here’s the full source code:

object Values {
  def main(args: Array[String]): Unit = {
    // Strings, which can be concatenated with +
    println("scala" + "lang")

    // Integers and floats
    println("1+1 = " + (1 + 1))
    println("7.0/3.0 = " + (7.0 / 3.0))

    // Booleans, with boolean operators as you'd expect
    println(true && false)
    println(true || false)
    println(!true)
  }
}

To run the program, save it in a file named Values.scala and use the scala command:

$ scala Values.scala
scalalang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

This example demonstrates:

  1. String concatenation using the + operator.
  2. Basic arithmetic operations with integers and floating-point numbers.
  3. Boolean operations including AND (&&), OR (||), and NOT (!).

In Scala, we don’t need to import a specific package for basic printing functionality as println is available by default. The main method is defined within an object, which is Scala’s way of implementing static methods and fields.

Scala is a statically typed language, but it often infers types automatically. In this example, we didn’t need to explicitly declare types for our literals, as Scala could infer them from context.