🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Kotlin, val and const are both used to declare variables that cannot be reassigned after initialization, but they have distinct characteristics and uses. The val keyword declares a read-only property or local variable. Meanwhile, const is used to define compile-time constants, meaning the value of a const variable is known at compile time and cannot be changed.
2. Key Points
1. Mutability: Both val and const are immutable, but val can have a custom getter with varying values.
2. Initialization: const must be initialized with a value known at compile time, val can be initialized with a runtime expression.
3. Usage Scope: const can only be used at the top level or in an object declaration, not in a class. val can be used anywhere.
4. Type Restrictions: const only allows primitives and String types, while val has no such restriction.
3. Differences
| Characteristic | Val | Const |
|---|---|---|
| Mutability | Immutable with a custom getter | Immutable, no custom getter |
| Initialization | Runtime expression | Compile-time expression |
| Usage Scope | Anywhere | Top-level or in an object |
| Type Restrictions | No restrictions | Primitives and String |
4. Example
// Example of Val
val pi: Double
get() = 3.14
// Example of Const
const val MAX_COUNT = 100
Output:
Val Output: pi will return 3.14 when accessed Const Output: MAX_COUNT will always be 100
Explanation:
1. pi is a val with a custom getter, allowing for complex logic. Its value is determined at runtime.
2. MAX_COUNT is a const, which means its value is set at compile time and cannot be changed.
5. When to use?
- Use val for read-only properties that might require complex initialization or a custom getter.
- Use const for defining constants that are primitives or Strings, whose value you know at compile time and that are not tied to a class instance.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment