In Kotlin programming, you can declare variables using either var or val keywords.
Var keyword is use to declare a variable and val declare a constant, can't be reassigned (they are called immutable).
For example:
val num = 11; println(num); val str = "Hello"; println(str);
If you'll try to reassign val
For an example:
val num = 11; Num = 12;
Kotlin type inference:
As you can see above examples, we didn't explicitly set type of the variables. The variables are getting their corresponding type, because of Kotlin Type Inference.
Follow the following syntax, to explicitly set the variables type:
val <variable name> : <variable type>=<value>
Example:
var num: Int = 42; val str: String = "Hello";
Comments
Post a Comment