HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Variables are containers for storing data values
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 (=
):
var variableName = value
val variableName = value
var name = "John"
val birthyear = 1975
println(name) // Print the value of name
println(birthyear) // Print the value of birthyear
The difference between var
and val
is that variables declared
with the var
keyword
can be changed/modified, while val
variables
cannot.
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).
To create a variable in Kotlin that should store text and another that should store a number, look at the following example:
var name = "John" // String (text)
val birthyear = 1975 // Int (number)
println(name) // Print the value of name
println(birthyear) // Print the value of birthyear
Kotlin is smart enough to understand that "John" is a String
(text), and that 1975 is an Int
(number) variable.
However, it is possible to specify the type if you insist:
var name: String = "John" // String
val birthyear: Int = 1975 // Int
println(name)
println(birthyear)
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:
This works fine:
var name: String
name = "John"
println(name)
This will generate an error:
var name
name = "John"
println(name)
Note: You will learn more about Data Types in the next chapter.
When you create a variable with the val
keyword, the value
cannot be changed/reassigned.
The following example will generate an error:
val name = "John"
name = "Robert" // Error (Val cannot be reassigned)
println(name)
When using var
, you can change the value whenever you want:
var name = "John"
name = "Robert"
println(name)
val
?The val
keyword is useful when you want a variable to always store the same value, like PI (3.14159...):
val pi = 3.14159265359
println(pi)
Like you have seen with the examples above, the println()
method is often used to display variables.
To combine both text and a variable, use the +
character:
val name = "John"
println("Hello " + name)
You can also use the +
character to add a variable to another variable:
val firstName = "John "
val lastName = "Doe"
val fullName = firstName + lastName
println(fullName)
For numeric values, the +
character works as
a mathematical operator:
val x = 5
val y = 6
println(x + y) // Print the value of x + y
From the example above, you can expect:
println()
method to display the value of x + y,
which is 11A variable can have a short name (like x and y) or more descriptive names (age, sum, totalVolume).
The general rule for Kotlin variables are:
var
or
String
) cannot be used as namesYou 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.