Scala Basic Syntax

Essential Scala syntax and program structure

Variables

val name = "John" # immutable variable
var age = 25 # mutable variable
val price: Double = 99.99 # explicit type
lazy val x = expensiveOp() # evaluated on first access

Data Types

Int # integer numbers
Long # long integers
Double # floating point
Float # 32-bit float
String # text values
Boolean # true or false
Char # single character
Unit # void equivalent

String Operations

val text = "Hello"
text.length # get length
text.toUpperCase # convert to uppercase
text.toLowerCase # convert to lowercase
s"Name: $name" # string interpolation
s"Sum: ${a + b}" # expression in string

Print Output

println("Hello World") # print with newline
print("Text") # print without newline

Comments

// single line comment
/* multi-line
   comment */