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:
To run the program, save it in a file named Values.scala
and use the scala
command:
This example demonstrates:
- String concatenation using the
+
operator. - Basic arithmetic operations with integers and floating-point numbers.
- 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.