In this tutorial, we’ll dive into variables, data types, and type inference in Kotlin. We’ll cover how to declare variables, assign values, and perform basic operations. Variables are used to store and manipulate data in a program. They act as named containers that hold values. In Kotlin, variables can be declared using the val and var keywords. Let’s start with Immutable variables.

patreon

 

 

 

Immutable Variables

Immutable variables are read-only, meaning their values cannot be changed once assigned. To declare an immutable variable, use the val keyword followed by the variable name and type (optional), and assign a value to it.

Here is an example:

In this example, hiScore is an immutable type Int (integer) variable that holds the value 100. Since it’s an immutable variable, its value cannot be changed. Of course, sometimes we will need to change the value of our variables.

Mutable Variables

Mutable variables are mutable, meaning their values can be changed after declaration. To declare a mutable variable, use the var keyword followed by the variable name and type (optional), and assign a value to it.

This is an example of a mutable variable:

Here, currentScore is a mutable type Int (integer) variable that holds the value 50. Being a mutable variable, its value can be modified later in the program.

Variables are fundamental to programming as they allow you to store and manipulate data throughout the execution of your program. Think of them as named storage locations that hold information.

Data Types

Kotlin provides various data types to represent different kinds of values. Let’s explore some commonly used data types:

Numeric Data Types:

Byte: 8-bit signed integer, ranging from -128 to 127.
Short: 16-bit signed integer, ranging from -32,768 to 32,767.
Int: 32-bit signed integer, ranging from -2,147,483,648 to 2,147,483,647.
Long: 64-bit signed integer, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Float: 32-bit floating-point number with decimal precision.
Double: 64-bit floating-point number with decimal precision.
Boolean Data Type:

Boolean: Represents logical values true or false. It’s useful for making decisions or storing conditions.

Character Data Type:

Char: Represents a single Unicode character. For example, ‘A’, ‘!’, or ‘😊’.

String Data Type:

String: Represents a sequence of characters enclosed in double-quotes. For example, “Hello, Kotlin!”.
Understanding the appropriate data types is crucial as they define the nature of the data being stored, allowing the program to allocate the right amount of memory and perform operations accordingly.

Here is a quick bit of code using each data type.

In the preceding snippet, we declare variables of the respective data types and assign them initial values. The val keyword indicates that these variables are immutable (read-only). The data types Byte, Short, Int, and Long represent signed integer values with different ranges. The Float and Double data types represent floating-point numbers with different levels of precision. Finally, the Boolean data type represents logical values (true or false).

Type Inference

Kotlin supports type inference, which means the compiler can automatically infer the type of a variable based on the assigned value. This allows you to omit the explicit type declaration. Look at this code and spot the difference.

In this case, the compiler automatically infers that hiScore is of type Int.

In this tutorial, we explored variables, data types, and type inference in Kotlin. We learned how to declare variables, assign values, and perform basic operations. Variables are fundamental to programming as they act as named containers for storing and manipulating data throughout the execution of a program.

We covered the two types of variables in Kotlin: val for immutable variables and var for mutable variables. Immutable variables hold values that cannot be changed once assigned, while mutable variables allow their values to be modified.

We also discussed commonly used data types in Kotlin, including numeric data types like Byte, Short, Int, Long, Float, and Double. Additionally, we explored the Boolean data type for logical values, the Char data type for single characters, and the String data type for sequences of characters.

Furthermore, we touched upon type inference, which allows the Kotlin compiler to automatically infer the type of a variable based on the assigned value. This feature reduces the need for explicit type declarations.

Now, let’s move on to the next tutorial, where we’ll explore control flow statements, such as conditional statements and loops. These constructs enable us to make decisions and iterate over code blocks, adding more functionality and flexibility to our programs. Kotlin Control Flow: Conditional Statements and Loops