🎓 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, var and val are keywords used to declare variables. Understanding the distinction between them is crucial for proper Kotlin programming. The var keyword declares a mutable variable - one that can be changed after it's initialized. The val keyword, on the other hand, declares an immutable variable, meaning once it's assigned a value, it cannot be changed.
2. Key Points
1. Mutability: var is mutable, val is immutable.
2. Initialization: Both var and val must be initialized before use, but var values can be changed later.
3. Best Practices: Using val is recommended for variables that don’t need to be modified, as it enhances readability and maintains immutability.
4. Use in Functions: val is often used for values that are meant to remain constant throughout the function, while var is used for values that need to change.
3. Differences
| Characteristic | Var | Val |
|---|---|---|
| Mutability | Mutable (can be changed) | Immutable (cannot be changed) |
| Initialization | Can be reassigned | Cannot be reassigned after initialization |
| Best Practices | Use when value changes | Use for constant values |
| Use in Functions | For changing values | For constant values |
4. Example
// Example of Var
var mutableName: String = "John"
mutableName = "Doe" // Allowed
// Example of Val
val immutableName: String = "Jane"
immutableName = "Doe" // Error: Val cannot be reassigned
Output:
Var Output: Initial name: John, Changed name: Doe Val Output: Compilation error on trying to change the name
Explanation:
1. mutableName is declared with var and can be reassigned to "Doe" after initialization.
2. immutableName is declared with val and attempting to change its value results in a compilation error.
5. When to use?
- Use var for variables whose values are expected to change during the execution of a program.
- Use val for declaring variables whose values are not meant to change after initialization, to maintain immutability and improve code readability.
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