How to Declare A Variable In Kotlin?

10 minutes read

Declaring a variable in Kotlin is straightforward. The general syntax for declaring a variable is as follows:

1
var variableName: DataType = value


Here's a breakdown of each element:

  • var: This keyword is used to declare a mutable variable. Mutable variables can be reassigned later in the code.
  • variableName: Choose an appropriate name for your variable. It should follow Kotlin's naming conventions.
  • DataType: Specify the data type of the variable. It could be any valid data type in Kotlin, such as Int, String, Boolean, etc.
  • value: Optionally, you can assign an initial value to the variable. If assigned, the data type of the value should match the declared data type.


For example, let's declare a few variables of different data types:

1
2
3
var name: String = "John"    // Declaring a String variable named 'name'
var age: Int = 25           // Declaring an Int variable named 'age'
var isStudent: Boolean = true    // Declaring a Boolean variable named 'isStudent'


In Kotlin, you can also use type inference, where the compiler automatically infers the data type based on the assigned value. In such cases, you can omit the explicit data type declaration:

1
2
3
var name = "John"        // Compiler will infer it as a String
var age = 25           // Compiler will infer it as an Int
var isStudent = true   // Compiler will infer it as a Boolean


Using type inference can make your code more concise and readable.

Best Kotlin Books to Read in 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Rating is 4.9 out of 5

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

3
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.8 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

4
Kotlin in Action

Rating is 4.7 out of 5

Kotlin in Action

5
Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

Rating is 4.6 out of 5

Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

6
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.5 out of 5

Head First Kotlin: A Brain-Friendly Guide

7
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.4 out of 5

Kotlin Cookbook: A Problem-Focused Approach

8
How to Build Android Apps with Kotlin: A practical guide to developing, testing, and publishing your first Android apps, 2nd Edition

Rating is 4.3 out of 5

How to Build Android Apps with Kotlin: A practical guide to developing, testing, and publishing your first Android apps, 2nd Edition

9
Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

Rating is 4.2 out of 5

Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

10
Java to Kotlin: A Refactoring Guidebook

Rating is 4.1 out of 5

Java to Kotlin: A Refactoring Guidebook


How to declare a variable in Kotlin?

To declare a variable in Kotlin, you can use the var keyword for mutable variables or the val keyword for immutable variables. Here's an example:

1
2
3
4
5
6
7
// Mutable variable
var age: Int = 25
age = 26 // Value can be changed

// Immutable variable
val name: String = "John"
name = "Alex" // Compiler error, value cannot be changed


In the above example, age is a mutable variable of type Int that can be modified later, while name is an immutable variable of type String that cannot be reassigned a new value once it is initialized.


What is the syntax for declaring a variable in Kotlin?

In Kotlin, you can declare a variable using the following syntax:

1
var variableName: DataType = value


or

1
val variableName: DataType = value


Here, var is used to declare a mutable variable, and val is used to declare an immutable variable.


variableName is the name you assign to the variable, which can be any valid identifier. DataType is the type of the variable. value is the initial value assigned to the variable, which is optional. If not provided, a default value based on the data type will be assigned.


For example:

1
2
var age: Int = 25
val name: String = "John Doe"


In the above example, age is a mutable variable of type Int with an initial value of 25, and name is an immutable variable of type String with an initial value of "John Doe".


How to declare an integer variable in Kotlin?

In Kotlin, you can declare an integer variable using the "var" or "val" keyword along with the data type "Int".


Here is an example of declaring an integer variable with the "var" keyword:

1
var myVariable: Int = 10


In this example, "myVariable" is the name of the variable and "Int" is the data type. The value 10 is assigned to the variable at the time of declaration.


If you want to declare a read-only (immutable) integer variable using the "val" keyword, you can do it like this:

1
val myImmutableVariable: Int = 20


In this case, you can't reassign a different value to "myImmutableVariable" once it's been assigned.


You can also omit the data type "Int" and let the compiler automatically infer it from the assigned value:

1
2
var myVariable = 30
val myImmutableVariable = 40


In this example, the compiler will determine that the variables should be of type "Int" based on their assigned values.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

In Haskell, variables are immutable, meaning once a variable is assigned a value, its value cannot be changed. To declare a variable in Haskell, you can follow the syntax: variableName :: type variableName = value Here, variableName is the name of the variable...
To increment a variable in TensorFlow, you can follow these steps:First, import the required modules by including the following lines at the beginning of your code: import tensorflow as tf Define a TensorFlow variable using the tf.Variable() function. This var...
In Kotlin, extension functions allow you to add new functionality to existing classes without modifying their source code. They provide a way to extend the behavior of classes from external libraries or even built-in classes. Here is how you can define and use...