Scala - Variables and Data Types

Scala Variables

Variable is a name which is used to refer memory location. Some space in memory is reserved when a variable is created.
Types of variables in Scala
1. Mutable Variable 
It is a variable whose value can be changed. It is declared using var keyword.
Example -1:
var sum = 50  
sum = 60  // It works, No error.
Example-2:
   val sum:Int = 50  // Data type specified explicitly
Note : Scala is a type infers language. So it is not required to specify data type explicitly.
2. Immutable Variable
It is a variable whose value can not be changed. It is declared using val keyword.
Example-3:
   val sum = 50  
   sum = 50  // Error: reassignment to val

Data Types 

Data Type Default Value Size
Boolean False True or false
Byte 0 8 bit signed value (-27 to 27-1)
Short 0 16 bit signed value(-215 to 215-1)
Char '\u0000' 16 bit unsigned Unicode character(0 to 216-1)
Int 0 32 bit signed value(-231 to 231-1)
Long 0L 64 bit signed value(-263 to 263-1)
Float 0.0F 32 bit IEEE 754 single-precision float
Double 0.0D 64 bit IEEE 754 double-precision float
String Null A sequence of characters

Post a Comment

0 Comments