Declaring Variables
suggest changeIn Kotlin, variable declarations look a bit different than Java’s:
val i : Int = 42
- They start with either
val
orvar
, making the declarationfinal
(”value”) or variable. - The type is noted after the name, separated by a
:
- Thanks to Kotlin’s type inference the explicit type declaration can be obmitted if there is an assignment with a type the compiler is able to unambigously detect
Java | Kotlin |
—— | —— |int i = 42;
| var i = 42
(or var i : Int = 42
) |final int i = 42;
| val i = 42
|
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents