How to use val and var in Kotlin? Kotlin Variable Tutorial

preview_player
Показать описание
How to declare and assign variables in Kotlin? val or var - what to use?

In this video, I'm going to answer both questions with an introduction to variables in Kotlin. You can declare variables in two ways in Kotlin. Read-only (or immutable reference) variables with the val keyword and mutable variables with the var keyword. The syntax to create variables in Kotlin is easy. First goes val or var, then the name followed by a colon and whitespace, then the type, and finally follows the assignment. So you can create a read-only String in the following: val s = "Test". You can omit the type entirely, as the Kotlin compiler can infer it. vals cannot be reassigned, while vars can. Generally, we should prefer val to var, as it improves the structure, clearness, and readability of the code. Further, it helps to prevent race conditions and deadlocks.

00:00 Introduction
00:14 What are val and var?
00:38 val Example
01:56 var Example
03:58 val vs var

Thank you for watching!
Рекомендации по теме
visit shbcf.ru