
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
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 Articles
- 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 let in JavaScript
- Difference between Var and Dynamics in C#
- Difference between var and dynamic in C#
- Explain the difference between let and var in Swift
- What's the difference between "!!" and "?" in Kotlin?
- Difference between "fold" and "reduce" in Kotlin
- Difference between "*" and "Any" in Kotlin generics
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference between List and Array types in Kotlin
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- Difference between Java and Kotlin in Android with Examples
- Difference between a "class" and an "object" in Kotlin

Advertisements