[1] Scala Development Environment Setup Tutorial — Using IntelliJ IDEA [2] Scala Development Tutorial — Scala Basics: Data Types [3] Scala Development Tutorial — Scala Basics: Variables and Constants
Scala Data Types
| Type | Description |
|---|---|
| Byte | 8-bit signed two’s-complement integer. Range -128 to 127 |
| Short | 16-bit signed two’s-complement integer. Range -32768 to 32767 |
| Int | 32-bit signed two’s-complement integer. Range -2147483648 to 2147483647 |
| Long | 64-bit signed two’s-complement integer. Range -9223372036854775808 to 9223372036854775807 |
| Float | 32-bit single-precision floating-point per IEEE 754 |
| Double | 64-bit double-precision floating-point per IEEE 754 |
| Char | 16-bit unsigned Unicode character, range U+0000 to U+FFFF |
| String | a sequence of characters |
| Boolean | true or false |
| Unit | denotes no value, equivalent to void in other languages. Used as the return type of methods that return nothing. Unit has a single instance value, written as (). |
| Null | a trait whose only instance is null; a subclass of AnyRef, not AnyVal |
| Nothing | a trait, subclass of all types (including AnyRef and AnyVal); has no instances |
| None | one of the two subclasses of Option (the other being Some); used for safe function return values |
| Nil | a List of length 0 |
| Any | the superclass of all other classes |
| AnyRef | the base class of all reference classes in Scala |
From the table above we can see that everything in Scala is an object — there are no primitive types like in Java. Some things also differ from Java: Scala includes a few notions of “nothingness” (Null, null, Nil, Nothing, None, and Unit).
Null and null
Null is a trait (if you’re not familiar with traits, think of it as somewhat like an abstract class in Java). There does exist an instance of null — that is null. The literal null is not hard to understand; it’s the same as in Java. It’s the value of a reference that points to no object. So if you write a method that takes a Null-typed parameter, you can only pass two values: null itself, or a reference of type Null.
Nil
Nil is an empty list that wraps nothing. It’s a zero-length list; it doesn’t represent “nothingness” at all — it’s a List, just with no contents.
Nothing
Nothing is another trait that extends Any, the superclass of the entire Scala type system. Nothing is a subclass of every class: it’s a subclass of List, of String, of Int. Remember Nil? It’s a List[Nothing], and it’s empty. Because Nothing is a subclass of every class, Nil can serve as an empty list of strings, an empty list of Ints, an empty list of Any, and so on.
None
In Java, when there’s nothing to return we return null, but then using it may hit a NullPointerException, and we need try/catch to check for null. Scala has a built-in solution: you can return an Option[String]. Option is an abstract class with two subclasses — the class Some and the object None — so you can return a Some[String] or None, and easily check whether an object was returned.
Unit
Unit is the type of methods that return no value, like a void return type in Java — fairly easy to understand.
