Instruction 2
Mutable and Immutable Variables
In Kotlin, variables can be mutable or immutable. There are instances where a variable’s data will never change throughout the program. In such cases, you have to declare your variable with val. On the other hand, if the data in the variable can change, use var.
You can’t modify val variables. It’ll result in an error. As a rule of thumb, your variables should always be val unless you’re certain you’ll modify it later in the program.
In your program, include a variable with data of $100 as weekly interest. This amount will be added to your total weekly amount as interest. Make this a local function variable and add it as the first item in the main function:
fun main(args: Array<String>) {
val weeklyInterest = 100
// Rest of the code
}
After calculating the total weekly amount, add the interest:
val totalWeeklyAmount = mondayAmount + tuesdayAmount + wednesdayAmount + thursdayAmount + fridayAmount
totalWeeklyAmount = totalWeeklyAmount + weeklyInterest
Run the program. You get an error that says, Val cannot be reassigned. You have to change val totalWeeklyAmount to var totalWeeklyAmount to be able to update it.
Note: Note that an IDE like IntelliJ IDEA will not allow you to run the program at all if you try to reassign a val variable. IDEs provide checks like this and many more.
Using const variables
const is another Kotlin keyword used to indicate a variable that can’t be changed. The difference between const and val is that const variables are only allowed on top-level, named objects or companion objects. For a variable to be at such high scopes, it means it has to be accessible throughout the whole file and not in the function. Same as the val, any attempt to alter a const variable will result in an error.
Note: Though you could name
constvariables like other variables, notice the name of theconstreferring to weekly interest is “all caps” or all capital letters with the words separated by an underscore, ‘_’:WEEKLY_INTEREST. All caps and the underscore aren’t required but are used as a coding standard. By making theconstvariable name all caps and underscored, this coding standard lets anyone reading your code immediately know the variable is a constant without reviewing the declaration and that it has top-level or global statusconst val WEEKLY_INTEREST = 100.
Not every data type can be a const. Only “primitive” data types and Strings are allowed. You’ll learn more about primitive data types in the next lesson.
Modify your program, adding a weekly interest amount as a const, naming it WEEKLY_INTEREST, and updating the totalWeeklyAmount calculation. This time, declare the variable outside the main function :
const val WEEKLY_INTEREST = 100
fun main(args: Array<String>) {
// ...
totalWeeklyAmount = totalWeeklyAmount + WEEKLY_INTEREST
// Rest of code
}
Most of your variables, up until now, have been either strings or integers. These are just two types of data. Variables can hold even more. Continue to the next segment to learn more about the basic types in Kotlin.