Câu hỏi phỏng vấn Kotlin
Câu hỏi

Where should I use var and where val ?

Câu trả lời

Here's a concise answer to your Kotlin interview question:

When to use var and val in Kotlin

In Kotlin, you should use:

  • val (value) for variables that are immutable and don't change their value after initialization.
  • var (variable) for mutable variables that can be reassigned a new value during the program's execution.

Use val when:

  • The value is constant and doesn't change throughout the program's lifecycle.
  • You want to ensure that the variable's value remains the same to avoid unintended modifications.
  • The variable is used in a functional programming style, where immutability is preferred.

Use var when:

  • The value needs to be updated or reassi...
junior

junior

Gợi ý câu hỏi phỏng vấn

expert

What is the difference between * and Any in Kotlin generics?

middle

What is a difference between a class and object in Kotlin?

expert

What is a motivation to make classes final by default in Kotlin? Do you agree with that decision?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào