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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FSHk7I_Cpx&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&utm_medium=GabrielShanahan&utm_campaign=KotlinPrimer&utm_content=join-our-team&utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>