Values, Variables, and Types
Values
val x: Int = 42
-
valmeans immutable.It's like
constorfinalin other language. -
Intis optional, but it's recommended.The compiler can infer that the type of variable is
Int.
Variables
var aVariable: Int = 4
aVariable = 5
-
vardefines variable.We can assign a new value to it.
Changing Variables is side effects in functional programming.
In functional programming, we prefer Values over Variable because Values are immutable, and they won't cause side effects.
Types
-
BooleanIt only has 2 values,
trueorfalse. -
Char,StringCharis the single character. It's written between the single quotes.val aChar: Char = 'c' -
Int,Short,LongThey all represent the number.
Shortpresents 2 btyes, 16 bits, 2<sup>16</sup>.Intpresents 4 btyes, 32 bits, 2<sup>32</sup>.Longpresents 8 btyes, 64 bits, 2<sup>64</sup>. -
DoubleIt represents float number.
-
UnitIt's
voidin other languages.Side effect is an expression that returns
Unit.