Scala - Data Types



Scala has all the same data types as Java, with the same memory footprint and precision. Following is the table giving details about all the data types available in Scala −

Sr.No Data Type & Description
1

Byte

8 bit signed value. Range from -128 to 127

2

Short

16 bit signed value. Range -32768 to 32767

3

Int

32 bit signed value. Range -2147483648 to 2147483647

4

Long

64 bit signed value. -9223372036854775808 to 9223372036854775807

5

Float

32 bit IEEE 754 single-precision float

6

Double

64 bit IEEE 754 double-precision float

7

Char

16 bit unsigned Unicode character. Range from U+0000 to U+FFFF

8

String

A sequence of Chars

9

Boolean

Either the literal true or the literal false

10

Unit

Corresponds to no value

11

Null

null or empty reference

12

Nothing

The subtype of every other type; includes no values

13

Any

The supertype of any type; any object is of type Any

14

AnyRef

The supertype of any reference type

All the data types listed above are objects. There are no primitive types like in Java. This means that you can call methods on an Int, Long, etc.

Scala Basic Literals

The rules Scala uses for literals are simple and intuitive. This section explains all basic Scala Literals.

Integral Literals

Integer literals are usually of type Int, or of type Long when followed by a L or l suffix. Here are some integer literals −

0
035
21 
0xFFFFFFFF 
0777L

Floating Point Literal

Floating point literals are of type Float when followed by a floating point type suffix F or f, and are of type Double otherwise. Here are some floating point literals −

0.0 
1e30f 
3.14159f 
1.0e100
.1

Boolean Literals

The Boolean literals true and false are members of type Boolean.

Symbol Literals

A symbol literal 'x is a shorthand for the expression scala.Symbol("x"). Symbol is a case class, which is defined as follows.

package scala
final case class Symbol private (name: String) {
   override def toString: String = "'" + name
}

Character Literals

A character literal is a single character enclosed in quotes. The character is either a printable Unicode character or is described by an escape sequence. Here are some character literals −

'a' 
'\u0041'
'\n'
'\t'

String Literals

A string literal is a sequence of characters in double quotes. The characters are either printable Unicode character or are described by escape sequences. Here are some string literals −

"Hello,\nWorld!"
"This string contains a \" character."

Multi-Line Strings

A multi-line string literal is a sequence of characters enclosed in triple quotes """ ... """. The sequence of characters is arbitrary, except that it may contain three or more consecutive quote characters only at the very end.

Characters must not necessarily be printable; newlines or other control characters are also permitted. Here is a multi-line string literal −

"""the present string
spans three
lines."""

Null Values

The null value is of type scala.Null and is thus compatible with every reference type. It denotes a reference value which refers to a special "null" object.

Escape Sequences

The following escape sequences are recognized in character and string literals.

Escape Sequences Unicode Description
\b \u0008 backspace BS
\t \u0009 horizontal tab HT
\n \u000c formfeed FF
\f \u000c formfeed FF
\r \u000d carriage return CR
\" \u0022 double quote "
\' \u0027 single quote .
\\ \u005c backslash \

A character with Unicode between 0 and 255 may also be represented by an octal escape, i.e., a backslash '\' followed by a sequence of up to three octal characters. Following is the example to show few escape sequence characters −

Example

object Test {
   def main(args: Array[String]) {
      println("Hello\tWorld" );
   }
} 

When the above code is compiled and executed, it produces the following result −

Output

Hello   World

Type Hierarchy: Value and Reference

Scala has a well-defined hierarchy of data types. Basic data types are values in nature except String data type. These basic data types are also known as Value Classes which inherit from AnyVal type in Scala. Strings and other user-defined classes inherit from AnyRef type in Scala.

Type Hierarchy: Value and Reference

For example,

class Product(name: String, price: Double)
val product = new Product("Smartphone", 699.99)
val productName: String = product.name
val productPrice: AnyVal = product.price

val item = "Laptop"
val itemAsAnyRef: AnyRef = item

In Scala, all classes are subtypes of either AnyVal or AnyRef.

Any is the top type in Scala, the parent of all types. It has universal methods like equals(), hashCode(), and toString(). Any has two direct subclasses: AnyVal and AnyRef.

Conversions Between Types

(a) Conversion from and to Int

In Scala, you can change values between other types and Int according to rules.

For example,

val maxByteValue: Byte = 127
val byteFromInt = maxByteValue.toByte
val beyondByteValue: Int = 128
val wrappedByte = beyondByteValue.toByte // wrapped around
byteFromInt should be (127)
wrappedByte should be (-128)

You can not assign values beyond the limit that it can hold to a Byte. But, by using conversion functions like toByte() can get more predictable and logical results in Scala. Scala wraps the assigned value. These can be converted back to Int easily.

(b) Conversion from Char or String to Int

You can convert from Char or String to Int. For example,

val specialChar: Char = '(c)'
val charToInt = specialChar.toInt // Converts a Char to Int

val numberAsString = "42"
val stringToInt = numberAsString.toInt // Converts a String to Int

What if a String has non-numeric characters? Can we convert it to Int? No, we can't.

val nonIntegerString = "ABC".toInt

In this example, since "ABC" is not a valid integer, so if you try to convert it into Int using toInt(), it will raise a NumberFormatException.

(c) From Long to Int and vice-versa

Since Long data types can hold more integers than Int data type can store. Long can hold a much larger range of values. There are some rules for conversion from Long to Int and vice-versa.

For example,

val largeLong = 1234567890L // Long literal
val intFromLiteral = 123456 // No qualifier, therefore Int
val longFromLong = largeLong // i2 is a Long because RHS is a Long
val intFromLong = largeLong.toInt // Conversion helper function works
val shortFromLong = largeLong.toShort

largeLong should be (1234567890L)
intFromLiteral should be (123456)
longFromLong should equal(intFromLiteral.toLong)
intFromLong should equal(intFromLiteral)
shortFromLong should be (208)

Note that the short variable 'shortFromLong' results in 208 because it overflows the range of a Short, which can only hold values from -32,768 to 32,767.

Since Long can hold much more values than Int so there cannot be issues in conversion from Int to Long. But vice-versa is not true. Because conversion from Long to Int may lose precision. This is the reason that the compiler does not allow silent assignments and shows a type-mismatch error.

You can use the right toType() function only if you know what you are doing. When you assign a value beyond what a Short can hold (like 65536), the toShort() function reduces it to zero, so the value shrinks.

(d) Preventing Incorrect Conversions

Scala has rules for type conversions. You can use specific functions to check whether a conversion is fit or not.

For example,

val number1 = 42
val number2 = 256
number2.isValidByte should be (false)
number1.isValidByte should be (true)

val largeNumber1 = 65536
largeNumber1.isValidShort should be (false)

val doubleValue = 1.5E100
doubleValue.isValidInt should be (false)

You can use helper functions like isValid{type-name}() functions to check if value fits variable type range.

Advertisements