Basic Types in Scala

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Basic Types in Scala

Basically a Scala program describes the interaction between objects which interchange the messages. For example things like numbers, character and strings etc. are objects that can interact with other objects or similar types of objects.

Learn more about Significance Of Scala Training in this insightful blog now!

Objects with no internal structure that is with no components are said to be of a basic type.


class hierarchy of scala


Table: Basic types in Scala

Data type Range
Byte integers in the range from −128 to 127
Short integers in the range from −32768 to 32767
Int integers in the range from −2147483648 to 2147483647
Long integers in the range from −9223372036854775808 to9223372036854775807
Float largest positive finite float : 3.4028235×1038 and smallest positive finite nonzero float : 1.40×10−45
Double largest positive finite double:1.7976931348623157×10308 and the smallest positive finite nonzero double : 4.9×10−324
char Unicode characters with code points in the range from U+0000to U+FFFF
String a finite sequence of Chars
Boolean either the literal true or the literal false
Unit either the literal true or the literal false
Null null or empty reference
Nothing the subtype of every other type; includes no values
Any Any the supertype of any type; any object is of type Any
AnyRef The supertype of any reference type i.e. nonvalue Scala classes and user-defined classes


When declaring a variable or a constant use keyword var or the keyword val then the name of a variable or a constant, an equals sign (i.e., the symbol =) and then the value the variable or the constant. Also you can wite:


scala> val i : Byte = 32

i: Byte = 32


If the value does not agree with the type then a type error is occurred.

scala> var x=30

x: Int = 30


An integer literal that is suffixed by an L or an l is assumed to be of type Long:

scala> var x=3l

x: Long = 3


Numbers can be written as decimal, octal or hexadecimal numerals. Octal numbers can be written by adding prefix 0:

scala> 0755

res0: Int = 493


Hexadecimal numbers can be written by adding prefix 0x or 0X:

scala> 0x2009

res1: Int = 8201


A floating point number contains an integral part and an optional decimal point which is represented by a period character that may be followed by an optional fractional part, an optional exponent and an optional type suffix. The exponent is started with either the letter E or e.

The letters f and F represents a single precision floating point number whereas the letters d and D represents a double precision floating point number:

scala> 20.11

res4: Double = 20.11


scala> 15.2E10

res4: Double = 15.5E10


A character must be enclosed in single quotation marks:

scala> var a = ‘i’

a: Char = ‘i’


You cannot assign to a character variable a single quotation mark because it gives error. To solve this problem escape sequence is used which is started with /.

Table: Escape Sequence

Escape sequence Meaning
  Horizontal tab
  New line
Single quote
Double Quote
ooo Octal digit
uhhhh h is hexadecimal digit
f Form feed
  Carriage return


A String is a sequence of Chars that is enclosed in double quotes:

scala> "Scala"

res6: java.lang.String = Scala


The literals true and false denote truth and untruth, respectively. These literals are the only values of type Boolean:

scala> val T = true

T: Boolean = true


Unit Type contains only one value which is designated by two parentheses:

scala> var x = ()

x: Unit = ()