Scala Tutorial Scala References

Scala - Data Types



One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. Based on the data type of a variable, the operating system allocates memory to the variable and decides what can be stored in the reserved memory.

There are various data types in Scala which can be categorized as below:

Data TypesDescription
BooleanStores true or false values.
Byte8-bit signed integer value. It ranges from -128 to 127.
Short16-bit signed integer value. It ranges -32,768 to 32,767.
Int32-bit signed integer value. It ranges -231 to 231-1.
Long64-bit signed integer value. It ranges -263 to 263-1.
Float32-bit IEEE 754 single-precision float. It ranges 1.40129846432481707e-45 to 3.40282346638528860e+38.
Double64-bit IEEE 754 double-precision float. It ranges 4.94065645841246544e-324d to 1.79769313486231570e+308d.
Char16-bit unsigned Unicode character. It ranges 0 to 216−1.
StringA sequence of Char.
UnitA value type which carries no meaningful information.
NullA subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal null.
NothingA subtype of all types, also called the bottom type. There is no value that has type Nothing.
AnyThe supertype of all types, also called the top type. It has two direct subclasses: AnyVal and AnyRef.
AnyRefRepresents reference types. All non-value types are defined as reference types. Every user-defined type is a subtype of AnyRef.

In Scala, specifying a type explicitly is not required. For example: By default 10 will be an Int. If other data type like Byte, Long, or Short is required then it is required to specify the data type as shown below in the given example. Similarly Numbers with a decimal (like 10.0) will be Double by default. If Float data type is required then it is needed to specify the data type as shown in the example below.

As Int and Double are the default numeric types, you typically create them without explicitly declaring the data type.

object MainObject {
  def main(args: Array[String]) {      
    var i: Boolean = true;
    var j: Char = 'A';
    var k: Byte = 10;
    var l: Short = 10;
    var m: Int = 10;
    var n: Long = 10L;
    var o: Float = 10.5f;
    var p: Double = 10.5d; 
    var q: String = "Hello World";

    //printing data types
    println(s"i is a Boolean. i = $i"); 
    println(s"j is a Char. j = $j");
    println(s"k is a Byte. k = $k");
    println(s"l is a Short. l = $l");
    println(s"m is an Int. m = $m");
    println(s"n is a Long. n = $n");
    println(s"o is a Float. o = $o");
    println(s"p is a Double. p = $p");
    println(s"q is a String. q = $q");
  }
}

The output of the above code will be:

i is a Boolean. i = true
j is a Char. j = A
k is a Byte. k = 10
l is a Short. l = 10
m is an Int. m = 10
n is a Long. n = 10
o is a Float. o = 10.5
p is a Double. p = 10.5
q is a String. q = Hello World

5