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

How is it recommended to create constants in Kotlin?

middle

What is the difference between open and public in Kotlin?

middle

What is the purpose of Unit-returning in functions? Why is VALUE there? What is this V ALUE ?

Bình luận

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

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