Variables & Operators

In this lesson, you will learn about variables and operators in Kotlin. You will learn how to declare variables, assign values to variables, and use operators to perform operations on variables. We will also quickly cover comments.

Comments

Just to make sure everyone is fully able to read the examples below, a "comment" is text in a code file that gets ignored. In kotlin single line comments are made with // and everything on the line after this is ignored. Multi-Line comments start with /* and end with */, all text between those are ignored.

// This is a single line comment!

/*
This
Is
A
Multi-Line
Comment
 */

What is a Variable?

A variable is a storage location that holds a value. In Kotlin, you can declare a variable using the var keyword. The syntax for declaring a variable is as follows:

var variableName: DataType = value
  • var: keyword used to declare a variable
  • variableName: name of the variable
  • DataType: type of the variable
  • =: assignment operator
  • value: value assigned to the variable

When you use the var keyword to declare a variable, you can change the value of the variable later in the program.

Example

var number: Int = 10
println(number) // Output: 10
number = 20 // Will not cause an error
println(number) // Output: 20

In the example above, we declare a variable number of type Int and assign it a value of 10. We then print the value of number, which is 10. Next, we assign a new value of 20 to number, and print the value of number, which is now 20.

If you want to declare a variable that cannot be changed later in the program, you can use the val keyword. The syntax for declaring a constant variable is as follows:

val variableName: DataType = value

All other aspects of declaring a constant variable are the same as declaring a variable with the var keyword.

Example

val pi: Double = 3.14159
var radius: Double = 5.0

println(pi * radius * radius) // Output: The area of a circle with radius 5.0

radius = 10.0 // Will not cause an error
pi = 3.0 // Will cause an error

Data Types

In Kotlin, variables have data types that specify the type of data they can hold. The following table lists some of the common data types supported by Kotlin:

Data TypeDescription
IntWhole number, no decimal places
DoubleNumber with ~15 decimal places
FloatNumber with ~7 decimal places
Booleantrue or false
CharSingle character
StringSequence of characters

Example

var age: Int = 16
var pi: Double = 3.14159
var isStudent: Boolean = true
var initial: Char = 'A'
var name: String = "John Doe"

var height: Int = "Five Feet" // Causes an error because the data type is incorrect
var weight = 150 // Kotlin can infer the data type, so you don't need to specify it

Type Inference

When a variable is given a value immediately, with its declaration, the type of the variable will automatically be chosen, or inferred. However, it is often recommended to specify the data type to avoid confusion.

Nullability

In Kotlin, variables can be nullable, meaning they can hold a null value. To declare a nullable variable, you can use the ? operator after the data type. Be careful when using nullable variables, as they can cause unexpected errors if not handled properly.

Example

var name: String? = null
println(name) // Output: null

Operators

Operators are symbols that are used to perform operations on variables and values. Kotlin supports a variety of operators, including arithmetic, assignment, comparison, and logical operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on variables and values. The following table lists the arithmetic operators supported by Kotlin:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)

Example

var x: Int = 10
var y: Int = 5

println(x + y) // Output: 15

Assignment Operators

Assignment operators are used to assign values to variables. The following table lists the assignment operators supported by Kotlin:

OperatorDescription
=Assigns the value on the right to the variable on the left
+=Adds the value on the right to the variable on the left
-=Subtracts the value on the right from the variable on the left
*=Multiplies the variable on the left by the value on the right
/=Divides the variable on the left by the value on the right
%=Assigns the remainder of the division of the variable on the left by the value on the right to the variable on the left

Example

var x: Int = 10 // Assigns 10 to x
x += 5 // Adds 5 to x
println(x) // Output: 15

Comparison or Boolean Operators

Comparison or boolean operators are used to compare values and return a boolean (true/false) result. The following table lists the comparison operators supported by Kotlin:

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

var x: Int = 10
var y: Int = 5

println(x > y) // Output: true
println(x == y) // Output: false

Logical Operators

Logical operators are used to combine multiple boolean expressions and return a boolean result. The following table lists the logical operators supported by Kotlin:

OperatorDescription
&&Returns true if both values are true
\|\|Returns true if at least one value is true
!Returns true if the value is false

Example

var x: Int = 10
var y: Int = 5

println(x > y && x < 20) // Output: true
println(x > y || x == 5) // Output: true

String Formatting

Although not an operator, string formatting is a useful technique for combining strings and variables. In Kotlin, you can use string interpolation to insert variables into strings. To use string interpolation, prefix the variable name with a dollar sign ($). You may also enclose the variable name with curly-braces (e.g. ${myVar}).

Example

var name: String = "Alice"
var age: Int = 30

println("Name: $name, Age: $age") // Output: Name: Alice, Age: 30