Table of contents
1.
Introduction
2.
Kotlin Variables
2.1.
Variable Names
2.2.
Type Inference
3.
Variables Declaration
3.1.
Mutable variable
3.2.
Immutable variables
4.
Kotlin Basic Variable Types
4.1.
Number Type
4.2.
Character Type
4.3.
Boolean Type
4.4.
Kotlin Strings
4.5.
Kotlin Arrays
5.
Displaying the Variables
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Variables

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss variables in Kotlin. We will learn about mutable and immutable variable types in Kotlin. The blog also discusses various in-built data types for storing integers, floats, doubles, strings, etc., in Kotlin. In the next section, we will look at the naming convention of variables in Kotlin.

Kotlin Variables

Variables are container/storage locations used for storing data. When writing a Kotlin program, one must define all the variables before using them. The data of a variable can be changed/manipulated depending upon the type of the variable.

Variable Names

Some standard rules need to be kept in mind while naming variables in a Kotlin program. One should note here that good naming of variables increases the program's readability and thus improves the code quality. Following are some general rules for naming variables in Koltin:

  • The variable names should start with a letter, $ or _ symbol. It can contain symbols like digits, underscores, and dollar signs apart from the normal letter symbols.
  • Variable names cannot have whitespace in them. 
  • Variable names are case sensitive, i.e., “var1” and “Var1” denote two different variables.
  • Kotlin Keywords like Int or String cannot be used as variable names. For more details, check out our blog Kotlin Keywords.

Type Inference

It is good to declare a variable's data type before using it. But Kotlin provides some flexibility in this case. When a variable is declared without a data type, the Kotlin compiler uses type inference to deduce its data type. For example,

// The variable named student is assigned some text value enclosed within double quotes; therefore, the inferred type of this variable is String
 
var student = "Ninja"

Variables Declaration

While writing programs in Kotlin, one can follow two different types of variable declarations.

Mutable variable

It denotes those variables whose values can be changed after their declaration. Mutable variables are declared using the keyword “var”. This keyword denotes that the value of this variable may change in the subsequent program. For example,

// Kotlin program to demonstrate mutable variables
fun main(){
    var salary = 5000
    println("Old salary: $salary")
    // Salary doubles ... Increment !!!
    salary *= 2
    println("New salary: $salary")
}

Output:

Old salary: 5000
New salary: 10000

Immutable variables

It denotes those variables whose values can't be modified after their declaration. Immutable variables are declared using the keyword “val”. This keyword denotes that we cannot change the value of this variable in the following program. For example,

// Kotlin program to demonstrate immutable variables
fun main(){
    val gravity = 9.8 // It is a constant value, so it must be declared using the val keyword
    gravity = 4.6 // This will throw error
}

Output:

error: Val cannot be reassigned

Kotlin Basic Variable Types

The variables types are known at compile time as Kotlin is a statically typed language. The in-built data types can be categorized into the following categories:

Number Type

Numbers in Kotlin are similar to almost all modern-day programming languages. There are six in-built data types representing numbers. They are listed as follows:

  • Byte
    The data type Byte can hold values in the range [-128,127].
  • Short
    The data type Short can hold values in the range [-32768,32767].
  • Int
    The data type Int can hold values in the range [-2^31, 2^31-1].
  • Long
    The data type Long can hold values in the range [-2^63,2^63-1].
  • Float
    The data type Float can hold single-precision 32-bit floating-point numbers.
  • Double
    The data type Double can hold double-precision 64-bit floating-point numbers.

Character Type

The data type Char is used to represent a character in Kotlin. The following program illustrates Char data type in Kotlin.

// Kotlin program to demonstrate Char data type
fun main(){
    val ch: Char = 'a'
    println("The character is: $ch")
}

Output:

The character is: a

Boolean Type

The data type Boolean is used to store a true or false value in Kotlin. The following program illustrates Boolean data type in Kotlin.

// Kotlin program to demonstrate Boolean data type
fun main(){
    var flag: Boolean = false
    println("The variable flag is: $flag")
    flag = true
    println("The variable flag is updated to: $flag")
}

Output:

The variable flag is: false
The variable flag is updated to: true

Kotlin Strings

Strings are one of the basic data types in any programming language. Strings in Kotlin are represented using the String class. The String class in Kotlin is used to denote character strings. For example,

// Kotlin program to demonstrate String class
fun main(){
    var company: String = "Coding Ninjas"
    println("Welcome to $company")
}

Output:

Welcome to Coding Ninjas

For further reading about Strings in Kotlin, refer to the Kotlin Strings blog on the Coding Ninjas website.

Kotlin Arrays

Arrays in Kotlin are represented using the Array class. An array is a group of data of one single data type. For example, one can create an array of characters, strings, floats, etc.

Displaying the Variables

Variables can be displayed on the output screen/monitor using Kotlin in-built functions such as print() and println(). For example, see the following program that shows the value of a variable on the output screen.

// Kotlin program to demonstrate displaying the variables on console screen
fun main(){
    var website_name = "Code Studio"
    println("$website_name is the best platform to learn Computer Science fundamentals.")
}

Output:

Code Studio is the best platform to learn Computer Science fundamentals.

Frequently Asked Questions

  1. Can we use variables in Kotlin without declaring them first?
    No, Kotlin is a statically typed language. Therefore, it requires all variables to be declared before using them.
     
  2. Can we reassign the value of a variable in Kotlin?
    It depends upon the type of the variable. If the variable is declared using the "var" keyword, it can be reassigned, whereas if the variable is declared using the keyword “val”, it cannot be reassigned.
     
  3. Can I start a variable name with a digit in Kotlin?
    No, one cannot start a variable name with a digit. The variable names should begin with a letter$ or _ symbol.

Key Takeaways

Cheers if you reached here!! The purpose of this blog was to introduce you to the concept of variables and study the various in-built data types present in Kotlin. To read about the input/output of variables in Kotlin, check out our Kotlin I/O blog. 

Yet there is never an end to the learning process, so check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development and how to design the applications of the future. Until then, good luck!!

Live masterclass