val khai báo biến chỉ đọc, không thể gán lại sau khi khởi tạo (tương tự final trong Java), còn var khai báo biến có thể thay đổi giá trị.
- Dùng
vallàm mặc định giúp code an toàn hơn và phù hợp với nguyên tắc immutability trong kiến trúc MVVM. - Kotlin compiler khuyến khích dùng
valở mọi nơi có thể.
val declares a read-only variable that cannot be reassigned after initialization (like final in Java), while var declares a mutable variable.
- Using
valby default improves code safety and aligns with immutability principles used in MVVM architectures. - The Kotlin compiler encourages using
valeverywhere possible.