Scala types / variables

Since Scala is a JVM language it is not suprising to see that it has the same data types as Java.

The following table illustrates the common data types

Data Type Description
Byte 8 bit signed value. Range from -128 to 127
Short 16 bit signed value. Range -32768 to 32767
Int 32 bit signed value. Range -2147483648 to 2147483647
Long 64 bit signed value. -9223372036854775808 to 9223372036854775807
Float 32 bit IEEE 754 single-precision float
Double 64 bit IEEE 754 double-precision float
Char 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF
String A sequence of Chars
Boolean Either the literal true or the literal false
Unit Corresponds to no value
Null null or empty reference
Nothing The subtype of every other type; includes no values
Any The supertype of any type; any object is of type Any
AnyRef The supertype of any reference type

The items in this table ara all objects, which means you can call methods on them.

For example suppose we has declared an int value like this:

var _int = 23

We could then call methods on it like this:


This is somewhat different to Java where there are true primitives. Scalas approach uses objects known as RichXXX which have something called implicit coversions, which allows them to be used to interop with Java where you may need to pass a Java primitive (say as int)

For example there is a RichInt class in Scala, which you can read about here

There are quite a few RichXXX classes in Scala, I would urge you all to read about them.

There is a good discussion on this at StackOverflow:

For now lets see how we can declare some variables in Scala


Here are some valid ways to create integers

val _int1 = 0
val _int2 = 35
val _int3 = 21
val _int4 = 0xFFFFFFFF
val _int5:Long = 0XCAFEBABE

Floating Points

Here are some valid ways to create floating points

val _float1 = 0.0
val_float2 = 1e40f
val _float3 = 2.1543f
val _float4 = 6.0e600
val _float5 = .4


Here are some valid ways to create booleans

val _bool1 = false
val _bool2 : Boolean = true
val _bool = _bool1 && _bool2


Here are some valid ways to create characters

val _char1 = 'b'
val _char2 = '\u0037'
val _char3 = '\n'
val _char4:Char = '\t'


Here are some valid ways to create strings

val _string1 = "Hello,\nWorld!"
val _string2 = "This string contains a \" character."

Multiline strings

You can also create multiline strings like this

val _multiLineString ="""this string
                          is a long one, it has 
                          3 lines."""

String interpolation

Scala also support string interpolation, such that you can do things like this

val _string1 = "Hello,\nWorld!"
val _string2 = "This string contains a \" character."
val string3 = s"The value of string1 = $_string1, and string2 = $_string2"

Escape Sequences

The following escape sequences are valid

Escape Sequence 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 \


Var vs Val

There is one last thing I wanted to mention in this post, which is the difference between var and val in Scala.


Basically val is an immuatble object. That is you can change the value of it, but you will not be able to recreate a new object pointer (reference) for a val. This would be an error. A object assigned to a var can still have its internal state altered though.


Var on the other hand is mutable , and as such will let you assign a new object pointer (reference).

There is a great StackOverflow post on this, which I would encourage you all to read:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s