avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1616

Abstract

variable, whose value <b>cannot</b> be changed</li><li><code>var</code> creates a mutable variable, whose value <b>can</b> be changed</li></ul><p id="693f">As is the case with function declarations, types are specified <b>after</b> declarations.</p> <figure id="a446"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FSHk7I_Cpx&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FSHk7I_Cpx&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><h1 id="061d">val vs. var</h1><p id="1239"><a href="https://kotlinlang.org/docs/coding-conventions.html#immutability">Prefer immutability over mutability</a>. <a href="https://www.leadingagile.com/2018/03/immutability-in-java/">Why</a>? No but really, <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html">why</a>? No but like, really, <a href="https://ahdak.github.io/blog/effective-java-part-3/#item-17--minimize-mutability">why</a>?</p><p id="019d">It is the authors experience that it is almost never necessary to use <code>var</code> in regular code, although coding exclusively with <code>val</code> sometimes requires more effort (and that’s okay).</p><p id="0cc0">A situation one may encounter m

Options

utability is:</p><ul><li>in code that needs to be performant</li><li>when defining low-level “atomic” operations on a data structure, e.g. if you were, for whatever reason, implementing your own <code>count(predicate)</code> method, you would probably use a mutable counter</li></ul><h1 id="dc24">val vs. const val</h1><h2 id="aba7">val</h2><ul><li>Value can be determined at runtime, e.g. <code>val now = Instant.now()</code></li><li>Can be defined anywhere a variable declaration is legal</li><li>Can be initialized with any legal value</li></ul><h2 id="a09d">const val</h2><ul><li>Value must be determined at compile time — it is an actual constant, like <code>PI</code></li><li>Can only be defined on the top-level or inside an <code>object</code> (more about those <a href="https://readmedium.com/object-declarations-49f5786ff86b">later</a>)</li><li>Can only be initialized with <code>String</code> or a primitive type</li></ul><p id="b7a1">Go back to <a href="https://readmedium.com/basic-functions-e85e667b6a0e">Basic Functions</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to the <a href="https://readmedium.com/when-construct-c90606274897">When Construct</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Basic Variables

A brief rundown of variables in Kotlin

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Variable declarations start with the val or var keyword, followed by the variable name.

  • val creates an immutable variable, whose value cannot be changed
  • var creates a mutable variable, whose value can be changed

As is the case with function declarations, types are specified after declarations.

val vs. var

Prefer immutability over mutability. Why? No but really, why? No but like, really, why?

It is the authors experience that it is almost never necessary to use var in regular code, although coding exclusively with val sometimes requires more effort (and that’s okay).

A situation one may encounter mutability is:

  • in code that needs to be performant
  • when defining low-level “atomic” operations on a data structure, e.g. if you were, for whatever reason, implementing your own count(predicate) method, you would probably use a mutable counter

val vs. const val

val

  • Value can be determined at runtime, e.g. val now = Instant.now()
  • Can be defined anywhere a variable declaration is legal
  • Can be initialized with any legal value

const val

  • Value must be determined at compile time — it is an actual constant, like PI
  • Can only be defined on the top-level or inside an object (more about those later)
  • Can only be initialized with String or a primitive type

Go back to Basic Functions, jump to the Table of Contents, or continue to the When Construct.

Join me in Etnetera
Kotlin
Java
Learning
Immutability
Programming
Recommended from ReadMedium