Kotlin Kotlin for Java Developers Declaring Variables


Example

In Kotlin, variable declarations look a bit different than Java's:

val i : Int = 42
  • They start with either val or var, making the declaration final ("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

JavaKotlin
int i = 42;var i = 42 (or var i : Int = 42)
final int i = 42;val i = 42