Kotlin Variables


Variables are containers for storing data values.

  • To create a variable, use var or val, and assign a value to it with the equal sign (=)
  • The difference between var and val is that variables declared with the var keyword can be changed/modified, while val variables cannot.

Variable types: Unlike many other programming languages, variables in Kotlin do not need to be declared with a specified type (like "String" for text or "Int" for numbers, if you are familiar with those).

var name = "John"      // String (text)
val birthyear = 1992 // Int (number)

Kotlin is smart enough to understand that "John" is a String (text), and that 1992 is an Int (number) variable.

..

You can also declare a variable without assigning the value, and assign the value later. However, this is only possible when you specify the type:

var name: String
name = "John"
println(name)

..

So When To Use val?

The val keyword is useful when you want a variable to always store the same value, like PI (3.14159...)

The general rule for Kotlin variables are:
  • Variable Names can have a short name (like x and y) or more descriptive names (age, sum, totalVolume).
  • Names can contain letters, digits, underscores, and dollar signs
  • Names should start with a letter
  • Names can also begin with $ and _ (but we will not use it in this tutorial)
  • Names are case sensitive ("myVar" and "myvar" are different variables)
  • Names should start with a lowercase letter and it cannot contain whitespace
  • Reserved words (like Kotlin keywords, such as var or String) cannot be used as names
  • CamelCase variables
You might notice that we used firstName and lastName as variable names in the example above, instead of firstname and lastname. This is called "camelCase", and it is considered as good practice as it makes it easier to read when you have a variable name with different words in it, for example "myFavoriteFood", "rateActionMovies" etc.

..

Comments