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

Explain the difference between lateinit and lazy in details

middle

Explain the null safety in Kotlin

junior

What is basic difference between fold and reduce in Kotlin? When to use which?

Bình luận

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

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