

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between "var" and "val" in Kotlin?
In Kotlin, we can declare a variable using two different keywords: one is var and the other one is val. In this article, we will take an example and demonstrate how these declarations are different from each other.
Attribute | var | val |
---|---|---|
Declaration | var varName="hello World" | val sName = "tutorialspoint.com" |
Immutability | Mutable | Immutable |
No. of times a variable can be assigned | Can be assigned multiple times. | Cannot be assigned multiple times. |
Reassigned | Can be reassigned | Cannot be reassigned |
Example
In the following example, we will see how we can declare two different variables using "val" and "var". We will also see that the variable declared using 'var' can be changed, while the variable declared using 'val' cannot be reassigned as it will throw an error at runtime with the error message "Val cannot be reassigned."
val sName = "tutorialspoint"; var varName = "hello World" fun main() { println("Example of val--->"+sName); println("Example of Var--->"+varName); // Variable declared by var is mutable varName = "new value"; println("New value of the variable declared using Var: " +varName); }
Output
It will generate the following output −
Example of val--->tutorialspoint Example of Var--->hello World New value of the variable declared using Var: new value
Example
Now, let's try to change the value of the variable declared using val −
val sName = "tutorialspoint"; var varName = "hello World" fun main() { println("Example of val--->"+sName); println("Example of Var--->"+varName); // Variable declared by val is not mutable sName = "new value"; println("New value of the variable declared using Var: " +sName); }
Output
It will throw an error at runtime −
main.kt:9:5: error: val cannot be reassigned sName = "new value"; ^
- Related Questions & Answers
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between VAR and DYNAMIC keywords in C#?
- What is the difference between: var functionName = function() {} and function functionName() {} in Javascript
- Difference between Var and Dynamics in C#
- Difference between var and dynamic in C#
- Difference between var and let in JavaScript
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- What is the difference between == and === in JavaScript?
- What is the difference between $ and @ in R?
- What's the difference between "!!" and "?" in Kotlin?
- What is the difference between = and: = assignment operators?
- What is the difference between g++ and gcc?
- What is the difference between Hinduism and Buddhism?
- What is the difference between IAS and IPS?
- What is the difference between hastily and quickly?
Advertisements