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

middle

Explain advantages of when vs switch in Kotlin

middle

When to use lateinit over lazy initialization in Kotlin?

senior

How can I create static method for enum in Kotiln?

Bình luận

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

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