Introduction to Variables, Types, and Literals

Understand variables by exploring concepts such as declaration, initialization, types, and literals.

Variable declaration

To declare a variable in Kotlin, we start with the val or var keyword, then a variable name, the equality sign, and an initial value.

  • The keyword var (which stands for variable) represents read-write variables and is used to define variables whose values can be reassigned after initialization. This means that if we use var, we can always assign a new value to this variable.

  • The keyword val (which stands for value) represents read-only variables and is used to define values that cannot be reassigned. This means that if we use val, we cannot assign a new value to this variable once it is initialized.

Press + to interact
fun main() {
val a = 10
var b = "ABC"
println(a) // 10
println(b) // ABC
// a = 12 // is not possible because a is read-only!
b = "CDE"
println(b) // CDE
}

Variable naming conventions

We can name variables using characters, underscore (_), and numbers, but numbers are not allowed at the first position. By convention, we name variables with the camelCase convention; this means the variable name starts with a lowercase letter, and then (instead of using spaces) each next word starts with a capital letter.

Press + to interact
In Kotlin, variable names follow the camelCase convention
In Kotlin, variable names follow the camelCase convention

Implicit variable type

Variables don’t need to specify their type explicitly, but this doesn’t mean that variables are not typed. Kotlin is a statically typed language, therefore, every variable needs its type specified. The point is that Kotlin is smart enough to infer the type from the value that is set. In the above code, 10 is of type Int, so the type of a is Int. Additionally, "ABC" is of type String, so the type of b is String.

Press + to interact
Kotlin’s implicit typing infers variable types from values
Kotlin’s implicit typing infers variable types from values

Note: In IntelliJ IDEA, when we type the variable name a, it automatically displays a suggestion dropdown, as illustrated above.

Explicit variable type

We can also specify a variable type explicitly using a colon and a type after the variable name.

Press + to interact
fun main() {
val a: Int = 10
var b: String = "ABC"
println(a) // 10
println(b) // ABC
b = "CDE"
println(b) // CDE
}

Line 2: We declare the variable a and initialize it with an Int type, allowing it to hold integer values. We assign the value 10 using the val keyword, which signifies that a is an immutable (read-only) variable. Its value cannot be changed once it’s assigned.

Line 3: We declare the variable b and initialize it as a String type capable of storing text or string values. We assign the value ABC. In contrast to val, we employ var to declare b as a mutable variable, allowing us to modify its value during the program’s execution.

Variable initialization

When we initialize a variable, we should give it a value. As shown in the example below, a variable’s definition and initialization can be separated if Kotlin can be sure that the variable won’t be used before any value is set. We suggest avoiding this practice when it’s not necessary.

Press + to interact
fun main() {
val a: Int
a = 10
println(a) // 10
}

We can assume that a variable should normally be initialized by using an assignment operator (=) after its declaration (like in val a = 10). So what can stand on the right side of the assignment? It can be any expression, i.e., a piece of code that returns a value. Here are the most common types of expressions in Kotlin we will explore in the course:

  • A basic type literal, such as 1 or "ABC"

  • A conditional statement used as an expression, such as the if, when, or try-catch expressions

  • A constructor call

  • A function call

  • An object expression or an object declaration

  • A function literal, such as a lambda expression, an anonymous function, or a function reference

  • An element reference

Basic types, their literals and operations

Every language needs a convenient way to represent basic kinds of values, like numbers or characters. All languages need to have built-in types and literals. Types are used to represent certain types of values. Some type examples are Int, Boolean, or String. Literals are built-in notations that are used to create instances, for example, a string literal, which is text in quotation marks, or an integer literal, which is a bare number.

We’ll learn about the basic Kotlin types and their literals:

  • Numbers (Int, Long, Double, Float, Short, Byte)

  • Booleans (Boolean)

  • Characters (Char)

  • Strings (String)

Points to remember:

  • In Kotlin, all values are considered objects (there are no primitive types), so they all have methods, and their types can be used as generic type arguments.

  • Types that represent numbers, booleans, and characters might be optimized by the Kotlin compiler and used as primitives, but this optimization does not affect Kotlin developers, therefore, we don’t even need to think about it.