In Scala, you can declare values and variables. Values are immutable (cannot be changed), while variables are mutable (can be changed).
// Declaring a value
val x: Int = 10
// Declaring a variable
var y: String = "Hello"
// Type inference
val z = 3.14 // Scala infers that z is a Double
Scala has several built-in data types, including:
Int
: For integers (e.g., 1, -5, 1000)Double
: For floating-point numbers (e.g., 3.14, -2.5)Boolean
: For true/false valuesString
: For textScala supports standard arithmetic and logical operations.
val sum = 5 + 3
val product = 4 * 2.5
val isEqual = (5 == 5)
String interpolation allows you to embed variables directly into strings.
val name = "Alice"
val greeting = s"Hello, $name!"
You can print to the console using println
and read input using scala.io.StdIn.readLine
.
println("Enter your name:")
val input = scala.io.