Scala Data Types
Scala Data Types
Section titled “Scala Data Types”Scala has a rich type system where every value is an object. Unlike Java’s distinction between primitive types and object types, Scala unifies these concepts. Basic data types like Int, Double, Boolean are classes, inheriting from AnyVal.
Value Types (AnyVal):
Section titled “Value Types (AnyVal):”These correspond roughly to Java’s primitives but are classes in Scala. They offer performance benefits as the runtime can often optimize them to underlying primitives.
| Data Type | Description |
|---|---|
| Byte | 8-bit signed integer. Range: -128 to 127 |
| Short | 16-bit signed integer. Range: -32,768 to 32,767 |
| Int | 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647 |
| Long | 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use L suffix (e.g., 100L). |
| Float | 32-bit IEEE 754 single-precision floating-point. Use F suffix (e.g., 3.14F). |
| Double | 64-bit IEEE 754 double-precision floating-point. Default for decimal literals (e.g., 3.14). |
| Char | 16-bit unsigned Unicode character. Range: U+0000 to U+FFFF. Enclosed in single quotes (e.g., 'A'). |
| Boolean | Represents logical values: true or false. |
| Unit | Represents non-returning computations (like void). Has only one value: (). Inherits from AnyVal. |
Reference Types (AnyRef):
Section titled “Reference Types (AnyRef):”These correspond to Java’s object types. All user-defined classes, traits, and standard library classes like String, List, Option inherit from AnyRef (which is an alias for java.lang.Object).
| Common Reference Type | Description |
|---|---|
| String | A sequence of characters. Backed by java.lang.String. Immutable. |
| All user-defined classes | Instances of classes you define (e.g., class Person(...)). |
| Standard Collections | List, Vector, Map, Set, Option, etc. |
| Null | A type whose only instance is null. It’s a subtype of all AnyRef types. Avoid using null; prefer Option. |
| Nothing | A trait that is a subtype of every other type, including AnyVal. It has no instances. Useful for signaling abnormal termination (e.g., throw exception) or for empty collections (List[Nothing]). |
Scala Type Hierarchy:
Section titled “Scala Type Hierarchy:”All types inherit from a common root class Any. Any has two direct subclasses: AnyVal (for value types) and AnyRef (for reference types).
AnyAnyValDouble,Float,Long,Int,Short,Byte,Char,Unit,Boolean
AnyRef(alias forjava.lang.Object)String,List,Option, All user classes, etc.Null(subtype of allAnyRef)
Nothing(subtype of everything, includingAnyValandNull)
Literals:
Section titled “Literals:”Literals are fixed values represented directly in code.
Integer Literals:
Section titled “Integer Literals:”Default to Int. Use L or l suffix for Long.
val intLiteral = 42val longLiteral = 1234567890Lval hexLiteral = 0x2A // Hexadecimal (equals 42)val octalLiteral = 052 // Octal (equals 42) - Less commonFloating-Point Literals:
Section titled “Floating-Point Literals:”Default to Double. Use F or f suffix for Float.
val doubleLiteral = 3.14159val anotherDouble = 1.0e20 // Scientific notationval floatLiteral = 2.718FBoolean Literals:
Section titled “Boolean Literals:”Simply true and false.
val isScalaFun = trueval isComplex = falseCharacter Literals:
Section titled “Character Literals:”Single character enclosed in single quotes.
val charA = 'A'val newlineChar = '\n' // Escape sequenceval unicodeChar = '\u0041' // Unicode for 'A'String Literals:
Section titled “String Literals:”Sequence of characters in double quotes.
val greeting = "Hello, world!\n" // Standard string with escape
// Multi-line strings using triple quotesval multiLine = """This stringspans multiplelines."""
// Multi-line strings with margin strippingval indented = """|SELECT | name, | age |FROM users |WHERE active = true""".stripMarginSymbol Literals:
Section titled “Symbol Literals:”Deprecated in Scala 2.13 and removed in Scala 3. Symbols were used for identifiers that could be created dynamically, often in reflection or specific DSLs. Use strings instead.
// 'mySymbol // Deprecated/Removed syntaxNull Value:
Section titled “Null Value:”The null literal belongs to the type Null and can be assigned to any AnyRef type. However, its use is discouraged in idiomatic Scala. Prefer Option[T] to represent the potential absence of a value.
val name: String = null // Possible, but discouragedval age: Option[Int] = None // Preferred way for absenceEscape Sequences:
Section titled “Escape Sequences:”Standard escape sequences can be used within Char and String literals:
| Sequence | Description |
|---|---|
| \b | Backspace |
| \t | Tab |
| \n | Newline |
| \f | Form feed |
| \r | Carriage return |
| ” | Double quote |
| ’ | Single quote |
| \ | Backslash |
Unicode characters can also be represented using \uxxxx.