Kotlin Variables & Usage Examples | Android Introduction #7

ibrahimcanerdogan
4 min readJan 17, 2024

--

In this reading, you’ll learn about the differences between variables and values and their associated keywords, var and val. You will also learn about variables and types and how they can make reading your own or anyone else’s code easier.

UDEMY COURSE

What are variables?

Since a variable is an alternative name for a value, when you place another variable on the right side of a value assignment, the value of the variable is assigned, not the variable itself. For example, if you have a variable a with value 10, and you assign it to a variable b (with val b = a), it is the value 10 that gets assigned. That means changes in a will not influence the value of b.

fun main() {
var a = 10
var b = a
a = 20
//Changing a does not change b
println(b) // 10
}

Tip: You can use println or print to output content to the console.

Using println(“test”) will print data to a new line. You can use it to display a value on the screen with a line break after it.

Using print(“test”) will output your data (in this case the word ‘test’) but it will not place a blank line after it.

Understanding val and var

When you define variables, you must use one of two keywords:

  • val is an abbreviation of ‘value’. You use this modifier to specify variables that cannot be reassigned. In other words, code will not start if you try to assign a new value. As a consequence, they always represent the value that was assigned when the variables were created. That is why it makes sense to say that such variables represent a single value.
  • var is an abbreviation of ‘variable’. You use this modifier to specify variables that can be reassigned. So their value can change when the code runs, for example, being initially assigned a value of zero, then later being assigned a value of 1.
fun main() {
// ERROR! val cannot be reassigned
val value = "A"
value = "B"

// OK! var can be assigned
var variable = "A"
variable = "B"
}

You might be wondering why val is ever used since var appears to be a more flexible way to assign a value. The answer is readability, which means the ease with which a reader can understand the written code.

When you use val, it can be reassuring to know that the initial value stays the same. When you see var, you know it might change, so you need to be aware of how it changes. With val, it is enough to view its declaration to know what the value is.

That is why if you do not plan to change a variable value, it is preferable to use val. Using val and minimizing the number of unnecessary variable changes will increase the readability of your code.

Variables and types

Each variable has a specific type. Variables of a certain type can only keep values of this type. For instance, an Int variable can store whole numbers but cannot store text.

By default, the type of the variable is the type of the value assigned to this variable during creation. Variable type cannot change once assigned. For instance, if you define a variable called str with an initial value of type string, like ABC, you can later assign str a different value of type string, but not of any other type.

fun main() {
var str = "ABC" // The type of str is String
str = "XYZ"
str = 42 // ERROR! Int value cannot be assigned to a variable of type String
str = true // ERROR! Boolean value cannot be assigned to a variable of type String
}

To specify the type, you use a colon and the name of the required type after the variable name.

fun main() {
var str: String = "ABC"
str = "XYZ"
}

There are a few reasons to use the explicit variable type. One is readability. Sometimes, the right side of the assignment might be complicated, and specifying a type to makes it easier to understand your code. Normally one might expect someone’s age to be stored as a numeric type. However, when the requirement is to combine the numeric with text, it’s better to specify the variable age explicitly as a string type.

fun main() {
val age: String = "" + 42 + "!"
}

Another reason that you might explicitly specify a type is when you need a value to include multiple subtypes. For instance, any is a data type that includes all other types. So string, int and Boolean are all subtypes of any. That means that if you define a variable called userInputData with a type of any, you can assign it any value you’ve learned about so far.

For example, if you define a variable x with type any, you can assign to it any value you’ve learned about so far.

fun main() {
var x: Any = "ABC"
println(x) // ABC
x = 123
println(x) // 123
x = true
println(x) // true
}

In this reading, you learned about the differences between variables and values and their associated keywords, var and val. You also learned about variable data types and how explicitly assigning a type to a variable can improve the readability of code.

İbrahim Can Erdoğan

LINKEDIN

YOUTUBE

UDEMY

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet