var canAssign: Integer = 10;To sum up, you can declare a variable using a keyword var and second option is to use a keyword def. In case you use var keyword to declare a variable, you are creating a reassignable variable. In case you opt in for def keyword, you are creating an initialize-only variable.
def cannotAssign: Integer = 10;
canAssign = 20; // will work fine
cannotAssign = 20; // will result in compiler error
However, it must be noted here that although a def variable cannot be reassigned but the object it references can definately mutate i.e. change its contents. Therefore a def variable may be assumed as an immutable type.
Isn't it interesting!
0 comments:
Post a Comment