Article image Variables and Data Types in Kotlin

4. Variables and Data Types in Kotlin

Page 4 | Listen in audio

Kotlin, being a modern programming language, offers a rich set of features for Android app development. One of the foundational elements of any programming language is its handling of variables and data types. In Kotlin, variables are used to store information that can be referenced and manipulated in a program. Understanding how to effectively use variables and data types is crucial for any developer working with Kotlin.

In Kotlin, variables can be declared using two keywords: val and var. The choice between these two depends on whether you want the variable to be mutable or immutable. The val keyword is used to declare a read-only variable, which means once it is initialized, its value cannot be changed. This is similar to a final variable in Java. On the other hand, var is used to declare a mutable variable, allowing its value to be changed after it has been initialized.


val immutableVariable: Int = 10
var mutableVariable: Int = 15
mutableVariable = 20 // This is allowed
// immutableVariable = 25 // This will cause a compile-time error

Kotlin is a statically typed language, which means the type of every expression is known at compile time. However, Kotlin also provides type inference, allowing you to omit the explicit type declaration if it can be inferred from the context. This can make your code more concise and readable.


val inferredInt = 42 // The type is inferred as Int
val inferredString = "Hello, Kotlin!" // The type is inferred as String

Despite the convenience of type inference, there are situations where explicitly specifying the type can improve code clarity, especially when dealing with complex expressions or when the inferred type might not be immediately obvious to someone reading the code.

Kotlin supports a wide range of data types, divided into primitive types and reference types. The primitive types in Kotlin are: Int, Long, Short, Byte, Double, Float, Char, and Boolean. These types correspond to their Java counterparts but are always objects in Kotlin, meaning they come with a set of useful methods.

Here is a brief overview of Kotlin's primitive data types:

  • Int: Represents a 32-bit signed integer. Commonly used for integer arithmetic.
  • Long: Represents a 64-bit signed integer. Used when a wider range than Int is needed.
  • Short: Represents a 16-bit signed integer. Less commonly used due to its limited range.
  • Byte: Represents an 8-bit signed integer. Typically used for data streams and file I/O.
  • Double: Represents a 64-bit double-precision floating-point number. Used for precise calculations.
  • Float: Represents a 32-bit single-precision floating-point number. Used when precision is less critical.
  • Char: Represents a single 16-bit Unicode character.
  • Boolean: Represents a logical value, either true or false.

In addition to primitive types, Kotlin provides a rich set of reference types, including String, arrays, collections, and more. Strings in Kotlin are immutable sequences of characters, similar to Java. However, Kotlin enhances the functionality of strings with features such as string templates and multiline strings.


val name = "Kotlin"
val greeting = "Hello, $name!" // String template
val multilineString = """
    This is a
    multiline string
    in Kotlin.
""".trimIndent()

Kotlin's collections framework includes lists, sets, and maps, which can be either mutable or immutable. Immutable collections are preferred when you do not need to change the collection after it is created, as they are inherently thread-safe and can lead to more predictable code.


// Immutable list
val readOnlyList = listOf("Kotlin", "Java", "Swift")

// Mutable list
val mutableList = mutableListOf("Kotlin", "Java")
mutableList.add("Swift")

One of Kotlin's standout features is its null safety. In Kotlin, variables cannot hold a null value by default. To allow a variable to hold null, you must explicitly declare it as nullable by appending a question mark (?) to the type. This helps prevent null pointer exceptions, a common source of runtime errors in Java.


var nullableString: String? = "Hello"
nullableString = null // This is allowed

// Safe call operator
val length = nullableString?.length

// Elvis operator
val lengthOrZero = nullableString?.length ?: 0

In addition to nullable types, Kotlin provides several operators and functions to work with null values safely, such as the safe call operator (?.), the Elvis operator (?:), and the let function. These tools help developers write more robust and error-free code.

Another powerful feature in Kotlin is the ability to define custom data types using classes and data classes. Data classes are a concise way to create classes that primarily hold data, providing automatically generated methods such as equals(), hashCode(), and toString().


data class User(val name: String, val age: Int)

val user = User("Alice", 30)
println(user) // Output: User(name=Alice, age=30)

Kotlin also supports type aliases, which allow you to create a new name for an existing type. This can be useful for improving code readability, especially when dealing with complex types or when you want to give a meaningful name to a type that is used in a specific context.


typealias UserName = String
val userName: UserName = "Alice"

In summary, Kotlin provides a robust and flexible system for handling variables and data types. Whether you're dealing with primitive types, collections, or custom data structures, Kotlin's features such as type inference, null safety, and data classes make it easier to write clean, concise, and safe code. Understanding these core concepts is essential for any developer looking to excel in Android app development using Kotlin.

Now answer the exercise about the content:

What keyword is used in Kotlin to declare a mutable variable whose value can be changed after initialization?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Control Flow in Kotlin

Next page of the Free Ebook:

5Control Flow in Kotlin

7 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text