
- 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 "const" and "val" in Kotlin?
const Keyword
The const keyword is used in Kotlin whenever the variable value remains const throughout the lifecycle of an application. It means that const is applied only on immutable properties of the class. In simple words, use const to declare a read-only property of a class.
There are some constraints that are applied to the const variable. They are as follows −
const can only be applied to the immutable property of a class.
It cannot be assigned to any function or any class constructor. It should be assigned with a primitive data type or String.
The const variable will be initialized at compile-time.
Example
In the following example, we will declare a const variable and we will use the same variable in our application.
const val sName = "tutorialspoint"; // This line will throw an error as we cannot // use Const with any function call. // const val myFun = MyFunc(); fun main() { println("Example of Const-Val--->"+sName); }
Output
It will yield the following output −
Example of Const-Val--->tutorialspoint
Val Keyword
In Kotlin, val is also used for declaring a variable. Both "val" and "const val" are used for declaring read-only properties of a class. The variables declared as const are initialized at the runtime.
val deals with the immutable property of a class, that is, only read-only variables can be declared using val.
val is initialized at the runtime.
For val, the contents can be muted, whereas for const val, the contents cannot be muted.
Example
We will modify the previous example in order to pass a function using val and we won't get any errors at runtime.
const val sName = "tutorialspoint"; // We can pass function using val val myfun=MyFunc(); fun main() { println("Example of Const-Val--->"+sName); println("Example of Val--->"+myfun); } fun MyFunc(): String { return "Hello Kotlin" }
Output
Once you execute the code, it will generate the following output −
Example of Const-Val--->tutorialspoint Example of Val--->Hello Kotlin
- Related Articles
- What is the difference between "var" and "val" in Kotlin?
- What is the difference between const int*, const int * const, and int const *?
- Difference between const int*, const int * const, and int const * in C
- What is the difference between #define and const Keyword in C++?
- What is the difference between keywords const and readonly in C#?
- Difference between const int*, const int * const, and int const * in C/C++?
- What is difference between int and const int& in C/C++?
- Difference between const char* p, char * const p, and const char * const p in C
- Difference between #define and const in C
- Difference between #define and const in Arduino
- Difference Between Static and Const in JavaScript
- Difference between readonly and const keyword in C#
- What's the difference between "!!" and "?" in Kotlin?
- Explain the difference between const and readonly keywords in C#
- Difference between "fold" and "reduce" in Kotlin
