Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
Scala is a statically-typed language, which means you must specify the type of a variable when you declare it, or it will be inferred by the compiler based on the initial value. Here are the most common data types in Scala:
Basic Data Types:
Byte: 8-bit signed two's complement integer (-128 to 127).
val b: Byte = 127
Short: 16-bit signed two's complement integer (-32,768 to 32,767).
val s: Short = 32767
Int: 32-bit signed two's complement integer.
val i: Int = 2147483647
Long: 64-bit signed two's complement integer.
val l: Long = 9223372036854775807L
Float: 32-bit IEEE 754 single-precision float.
val f: Float = 3.14f
Double: 64-bit IEEE 754 double-precision float.
val d: Double = 3.14159265359
Char: 16-bit unsigned Unicode character (U+0000 to U+FFFF).
val c: Char = 'A'
Boolean: true
or false
.
val isTrue: Boolean = true
String: It's not a basic type in the traditional sense, but it's used so frequently that it deserves mention. It's an immutable sequence of characters.
val str: String = "Hello, Scala!"
Arrays and Collections: Scala offers a variety of collection classes. The primary ones include List
, Set
, Map
, Seq
, Vector
, and Array
.
Array: Mutable, fixed-size.
val arr: Array[Int] = Array(1, 2, 3)
List: Immutable.
val lst: List[Int] = List(1, 2, 3)
Set: Collection of distinct elements. Can be mutable or immutable.
val s: Set[Int] = Set(1, 2, 2, 3) // Will result in Set(1, 2, 3)
Map: Collection of key-value pairs. Can be mutable or immutable.
val m: Map[String, Int] = Map("one" -> 1, "two" -> 2)
Option, Some, None: These types are used to represent the presence or absence of a value.
val opt1: Option[Int] = Some(42) val opt2: Option[Int] = None
Tuples: A tuple groups together simple logical collections of items without using classes. Tuples are immutable and can hold heterogeneous data types.
val tuple: (Int, String, Boolean) = (1, "Scala", true)
Function Types: Functions are first-class citizens in Scala, and they have their own types.
val func: Int => String = (x: Int) => x.toString
Remember that Scala is a rich language, and there are other types like case classes, objects, and various advanced collection types (like SortedSet
, Queue
, etc.). However, these are the fundamental building blocks for most applications.
Scala primitive data types:
val intVariable: Int = 42 val doubleVariable: Double = 3.14 val charVariable: Char = 'A' val booleanVariable: Boolean = true
Numeric data types in Scala:
Byte
, Short
, Int
, Long
, Float
, and Double
.val intValue: Int = 42 val floatValue: Float = 3.14f
String and character data types in Scala:
val str: String = "Hello, Scala!" val char: Char = 'A'
Scala Boolean data type:
val isTrue: Boolean = true val isFalse: Boolean = false
Tuples and tuple data types in Scala:
val myTuple: (Int, String, Double) = (1, "Scala", 3.14)
Scala collections data types:
List
, Set
, Map
, Array
, and more, for handling groups of elements.val myList: List[Int] = List(1, 2, 3) val mySet: Set[String] = Set("apple", "banana", "orange") val myMap: Map[String, Int] = Map("one" -> 1, "two" -> 2)
Option and Either data types in Scala:
Option
and Either
are used for handling optional and error-prone values in a functional way.val maybeValue: Option[Int] = Some(42) val result: Either[String, Int] = Right(42)
User-defined data types in Scala:
class
or case class
, enabling the definition of structured and meaningful entities.case class Person(name: String, age: Int) val john: Person = Person("John", 30)
Enumeration data types in Scala:
Enumeration
trait, providing a set of named values.object Color extends Enumeration { val Red, Green, Blue = Value } val myColor: Color.Value = Color.Red
Pattern matching with data types in Scala:
def matchExample(value: Any): String = value match { case s: String => s"String: $s" case i: Int => s"Integer: $i" case _ => "Other" }
Type inference in Scala:
val inferredInt = 42 val inferredString = "Scala"
Scala Any and AnyVal data types:
Any
is the root type of the Scala type hierarchy, and AnyVal
is the base type for value types (non-reference types).val anyValue: Any = "Any can hold any value" val anyValValue: AnyVal = 42