Trong Kotlin, biến mặc định không được phép null để loại bỏ NullPointerException.
- Muốn cho phép null, bạn khai báo kiểu nullable bằng
?(ví dụString?). - Compiler bắt buộc bạn xử lý null ngay lúc biên dịch qua các toán tử như
?.(safe call) hoặc!!(ép buộc). - Điều này giúp các lỗi liên quan đến null không thể lọt qua bước compile.
In Kotlin, variables are non-nullable by default to eliminate NullPointerException.
- To allow null values, declare a nullable type with
?(e.g.,String?). - The compiler forces you to handle null cases at compile-time using safe operators like
?.(safe call) or!!(unsafe cast). - This makes null-related bugs impossible to pass compilation.