Now that we’ve familiarized ourselves with the concept of variables, let’s learn how to create a variable in Kotlin. A complete variable declaration and initialization resembles the following form:
var variableName: Type = value
varkeyword specifies the beginning of a variable declaration. It signifies a mutable variable which means its value can change throughout the program.
Following the keyword is the name of the variable in
camelCaserefers to a naming convention where the first word is lowercased, the first letter of the second is capitalized, and so on. It’s important to create concise, yet descriptive variable names for readability and maintainability. A famous software engineer once said:
“..I will cheerfully spend literally hours on identifier names: variable names, method names, and so forth, to make my code readable…” - Joshua Bloch
The variable name is then followed by the type of data that the variable is intended to store. The type must always be capital.
- Once the variable has been declared, we can initialize it with a value. The
=sign, known as an assignment operator in programming, assigns a value to the variable.
Let’s apply this syntax to our first Kotlin variable:
var guitarName: String = "Fender Stratocaster"
guitarName variable has been allocated a position in memory by the compiler and can now be referenced and used throughout our program.
The compiler also recognizes that this variable is mutable due to the
var keyword ahead of the variable name; let’s explore what that means.
We may sometimes need to declare a variable in our program, knowing it will be of use later on, but don’t have a value to initialize it with just yet. We can do so in this way:
var variableName: Type
Assume we’re building a music application that will listen for the notes played on a guitar. We can declare a
notePlayed variable and initialize it with a value when the user plays a note:
var notePlayed: String // declaration notePlayed = "B" // initialization println(notePlayed) // Prints: B
Notice how we did not need to use the
var keyword on the second line as it is only used upon initial declaration. We’ve also omitted the type of data since this information needs to only be specified once.
main() function of Date.kt, declare a
String variable named
todaysDate and initialize it with today’s date using the following form:
On the following line, add a
println() statement and output the value of
On the following line, declare another
currentWeather. This time we will initialize the value separately, on the next line.
Take a peek outside, and then assign a weather description to
println()below the initialized value and output