<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mayank Kumar</title>
    <description>The latest articles on DEV Community by Mayank Kumar (@imayankkumar).</description>
    <link>https://dev.to/imayankkumar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4213841%2F55be9dc2-7e78-4246-bebd-171f578f09f5.jpg</url>
      <title>DEV Community: Mayank Kumar</title>
      <link>https://dev.to/imayankkumar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imayankkumar"/>
    <language>en</language>
    <item>
      <title>When Should You Use an Enum Class in Kotlin?</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Sat, 21 Nov 2026 18:53:46 +0000</pubDate>
      <link>https://dev.to/imayankkumar/when-should-you-use-an-enum-class-in-kotlin-2pf6</link>
      <guid>https://dev.to/imayankkumar/when-should-you-use-an-enum-class-in-kotlin-2pf6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; use an enum class when a value must be one of a small, fixed set that you know at compile time, and every option has the same shape. Each constant is a single object. It can carry values and its own behavior. When the options need different data, use a sealed class instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding a constant to an enum takes one line. Adding it in the wrong place can break data you saved last year.&lt;/p&gt;

&lt;p&gt;Have you ever saved an enum to SharedPreferences, shipped the app, and later added a constant in the middle of the list? The code still compiles. But every user who saved the old value now reads a different status, and nothing crashes to tell you.&lt;/p&gt;

&lt;p&gt;This post covers what an enum class is, how to add values and behavior to it, why &lt;code&gt;entries&lt;/code&gt; replaced &lt;code&gt;values()&lt;/code&gt;, and how to save an enum so it survives changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an enum class in Kotlin?
&lt;/h2&gt;

&lt;p&gt;An enum class is a class with a fixed list of instances. You write every instance inside the class, and nobody can create more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;PLACED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PREPARING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;OUT_FOR_DELIVERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;DELIVERED&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PREPARING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each constant is created once. &lt;code&gt;OrderStatus.PREPARING&lt;/code&gt; is always the same object.&lt;/p&gt;

&lt;p&gt;Every constant also gets two values for free:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;name&lt;/code&gt;:&lt;/strong&gt; the constant's name as a &lt;code&gt;String&lt;/code&gt;, like &lt;code&gt;"PREPARING"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ordinal&lt;/code&gt;:&lt;/strong&gt; its position in the list, starting from 0. &lt;code&gt;PREPARING&lt;/code&gt; is 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How do you add values and functions to an enum?
&lt;/h2&gt;

&lt;p&gt;An enum can have a constructor, properties and functions, like any class. Pass the values in brackets after each constant, and end the list with a &lt;code&gt;;&lt;/code&gt; before the members:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;PLACED&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"placed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;PREPARING&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"preparing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;OUT_FOR_DELIVERY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"out_for_delivery"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;DELIVERED&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"delivered"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;
        &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;DELIVERED&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each constant can also have its own behavior. Declare an &lt;code&gt;abstract&lt;/code&gt; function in the enum, and give each constant its own body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeliverySpeed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;STANDARD&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;deliveryCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderTotal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderTotal&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;EXPRESS&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;deliveryCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderTotal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;deliveryCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderTotal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;DeliverySpeed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STANDARD&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deliveryCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;650&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Standard delivery is free above 500, and express always costs 60. An enum can also implement an interface in the same way.&lt;/p&gt;

&lt;p&gt;What an enum can't do is extend another class. Every enum already extends &lt;code&gt;Enum&lt;/code&gt;, so Kotlin 2.4.21 stops you with "Enum classes cannot extend classes."&lt;/p&gt;

&lt;h2&gt;
  
  
  Why doesn't when need an else branch?
&lt;/h2&gt;

&lt;p&gt;The compiler knows every constant, so a &lt;code&gt;when&lt;/code&gt; over an enum can cover all of them without &lt;code&gt;else&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PLACED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Order placed"&lt;/span&gt;
    &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PREPARING&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Being prepared"&lt;/span&gt;
    &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OUT_FOR_DELIVERY&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"On the way"&lt;/span&gt;
    &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DELIVERED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Delivered"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;CANCELLED&lt;/code&gt; later, and this stops compiling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'when' expression must be exhaustive. Add the 'CANCELLED' branch or an 'else' branch.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That error is your to-do list. If you had written &lt;code&gt;else -&amp;gt; "Order placed"&lt;/code&gt;, a cancelled order would show "Order placed" and nothing would warn you. It's the same trap I covered in the &lt;a href="https://imayankkumar.com/blog/kotlin-sealed-class/#why-is-else-almost-right" rel="noopener noreferrer"&gt;sealed class post&lt;/a&gt;. Keep &lt;code&gt;else&lt;/code&gt; out of &lt;code&gt;when&lt;/code&gt; on enums and sealed types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you use entries or values()?
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;entries&lt;/code&gt;. It became &lt;a href="https://kotlinlang.org/docs/whatsnew19.html#stable-replacement-of-the-enum-class-values-function" rel="noopener noreferrer"&gt;stable in Kotlin 1.9.0&lt;/a&gt;, and the Kotlin team recommends it over &lt;code&gt;values()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;values()&lt;/code&gt; returns a new array on every call, because an array can be changed by whoever gets it. On Kotlin 2.4.21, &lt;code&gt;values() === values()&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;. &lt;code&gt;entries&lt;/code&gt; returns a read-only &lt;code&gt;List&lt;/code&gt; that is created once.&lt;/p&gt;

&lt;p&gt;For generic code, where you only know the enum as a type &lt;code&gt;T&lt;/code&gt;, use &lt;a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.enums/enum-entries.html" rel="noopener noreferrer"&gt;&lt;code&gt;enumEntries&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/a&gt; (stable since Kotlin 2.0). The older &lt;code&gt;enumValues&amp;lt;T&amp;gt;()&lt;/code&gt; has been around since Kotlin 1.1.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;API&lt;/th&gt;
&lt;th&gt;What it gives you&lt;/th&gt;
&lt;th&gt;When to use it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;entries&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A read-only &lt;code&gt;List&lt;/code&gt; of all constants, in order&lt;/td&gt;
&lt;td&gt;Loops, dropdowns, lookups&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;values()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A new &lt;code&gt;Array&lt;/code&gt; on every call&lt;/td&gt;
&lt;td&gt;Only in old code; move to &lt;code&gt;entries&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;valueOf(name)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The constant with that exact name, or an exception&lt;/td&gt;
&lt;td&gt;Names you control, never server data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;enumEntries&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;entries&lt;/code&gt; for a generic type &lt;code&gt;T&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Inline functions with &lt;code&gt;reified T&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The constant's name&lt;/td&gt;
&lt;td&gt;Logs and debugging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ordinal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The constant's position, from 0&lt;/td&gt;
&lt;td&gt;Sorting in memory, never saving&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Why is saving ordinal or name risky?
&lt;/h2&gt;

&lt;p&gt;Because both can change when you edit the enum, but the saved value doesn't. Your app saves a status today and reads it back after an update. If the enum changed in between, the old value now means something else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Saving &lt;code&gt;ordinal&lt;/code&gt; (the position).&lt;/strong&gt; &lt;code&gt;PREPARING&lt;/code&gt; is second in the list, so its ordinal is &lt;code&gt;1&lt;/code&gt;, and you save &lt;code&gt;1&lt;/code&gt;. In the next release you add &lt;code&gt;CONFIRMED&lt;/code&gt; after &lt;code&gt;PLACED&lt;/code&gt;. Now position 1 is &lt;code&gt;CONFIRMED&lt;/code&gt;. The app reads &lt;code&gt;1&lt;/code&gt; and shows "Confirmed" for an order that is being prepared. No crash, just wrong data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Saving &lt;code&gt;name&lt;/code&gt;.&lt;/strong&gt; You save &lt;code&gt;"PREPARING"&lt;/code&gt;. Later you rename the constant to &lt;code&gt;COOKING&lt;/code&gt;. Now &lt;code&gt;valueOf("PREPARING")&lt;/code&gt; finds nothing and throws, and the app crashes for every user who had the old value saved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reading server data with &lt;code&gt;valueOf()&lt;/code&gt;.&lt;/strong&gt; The server starts sending a new status, &lt;code&gt;"READY_FOR_PICKUP"&lt;/code&gt;. Older app versions don't have that constant, so &lt;code&gt;valueOf()&lt;/code&gt; throws there too. You can't fix apps that users haven't updated.&lt;/p&gt;

&lt;p&gt;The fix is to save a value you control and never change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right: breaks when the enum changes&lt;/span&gt;
&lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;edit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;putInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"last_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"last_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// Right: save a fixed code, and handle codes you don't know&lt;/span&gt;
&lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;edit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;putString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"last_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"last_status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PLACED&lt;/span&gt;

&lt;span class="c1"&gt;// inside OrderStatus&lt;/span&gt;
&lt;span class="n"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fromCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstOrNull&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;code&lt;/code&gt; is the &lt;code&gt;"preparing"&lt;/code&gt; string from the enum earlier. You can move or rename &lt;code&gt;PREPARING&lt;/code&gt;, and &lt;code&gt;"preparing"&lt;/code&gt; still finds it. For a code the app doesn't know, &lt;code&gt;fromCode()&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt; instead of crashing, and &lt;code&gt;?:&lt;/code&gt; picks a safe default. That's the same idea as the &lt;a href="https://imayankkumar.com/blog/kotlin-null-safety/" rel="noopener noreferrer"&gt;null safety post&lt;/a&gt;: make the unknown case something the compiler makes you handle.&lt;/p&gt;

&lt;p&gt;If you use kotlinx.serialization, &lt;code&gt;@SerialName("preparing")&lt;/code&gt; on each constant does the same job for JSON.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6end56l6q7bwvb5j54g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6end56l6q7bwvb5j54g.png" alt="Diagram: what breaks when you change an enum. Saving ordinal gives wrong data when you add or move a constant and a crash when an old app reads a new value. Saving name crashes when you rename a constant or an old app reads a new value. Saving your own fixed code survives all three changes." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Enum or sealed class?
&lt;/h2&gt;

&lt;p&gt;Use an enum when every option has the same shape: the same properties, filled with fixed values. Order statuses, sort orders and delivery speeds fit well.&lt;/p&gt;

&lt;p&gt;Use a sealed class when each option needs its own data. A &lt;code&gt;Failed&lt;/code&gt; state needs a message, and a &lt;code&gt;Ready&lt;/code&gt; state needs the items. An enum can't do that. Each constant is created once, so its values are fixed when you write the code, and an error message only exists at runtime.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://imayankkumar.com/blog/kotlin-sealed-class/" rel="noopener noreferrer"&gt;sealed class post&lt;/a&gt; covers that side in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Are enums still slow on Android?&lt;/strong&gt;&lt;br&gt;
Early Android advice said to use &lt;code&gt;Int&lt;/code&gt; constants instead. Today, R8 optimizes many enum uses in release builds. For example, it &lt;a href="https://jakewharton.com/r8-optimization-enum-ordinals-and-names/" rel="noopener noreferrer"&gt;replaces &lt;code&gt;ordinal&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; calls with constants&lt;/a&gt;. In normal app code, pick the enum for safety and clear code, and measure before you change it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can an enum extend a class?&lt;/strong&gt;&lt;br&gt;
No. It can implement any number of interfaces, but it can't extend a class, because it already extends &lt;code&gt;Enum&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can you compare enum constants with &lt;code&gt;==&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
Yes. Each constant is a single object, so &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;===&lt;/code&gt; both work and give the same answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;An enum is a fixed list of single objects that can hold values and behavior. &lt;code&gt;when&lt;/code&gt; checks that you covered every constant. Use &lt;code&gt;entries&lt;/code&gt;, not &lt;code&gt;values()&lt;/code&gt;. And never save &lt;code&gt;ordinal&lt;/code&gt; or &lt;code&gt;name&lt;/code&gt;: save a code you control, and handle codes you don't know.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-enum-class/" rel="noopener noreferrer"&gt;enum class interview questions&lt;/a&gt;. This post is the sixth topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;. The previous one is &lt;a href="https://imayankkumar.com/blog/kotlin-inner-vs-nested-class/" rel="noopener noreferrer"&gt;inner vs nested classes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Where does your app save an enum today, and what would happen if you added a constant in the middle?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-enum-class/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Inner vs Nested Class in Kotlin: What Is the Difference?</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Tue, 17 Nov 2026 19:30:36 +0000</pubDate>
      <link>https://dev.to/imayankkumar/inner-vs-nested-class-in-kotlin-what-is-the-difference-36a6</link>
      <guid>https://dev.to/imayankkumar/inner-vs-nested-class-in-kotlin-what-is-the-difference-36a6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; a nested class is just a class written inside another class. It has no link to any outer object. Mark it &lt;code&gt;inner&lt;/code&gt; and every instance keeps a hidden reference to the outer object. That lets it use the outer class's members, but it also keeps the outer object alive for as long as the inner one lives.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One keyword looks like a small change. In the bytecode, it adds a field to every object you create.&lt;/p&gt;

&lt;p&gt;Have you ever added &lt;code&gt;inner&lt;/code&gt; just to make a red error go away in Android Studio? It works, the code compiles, and you move on. But that keyword changes what your object holds in memory. In an Activity, that can be the difference between a clean screen and a leak.&lt;/p&gt;

&lt;p&gt;This post covers what a nested class is, what &lt;code&gt;inner&lt;/code&gt; adds, what the compiler generates for each, and when to use &lt;code&gt;inner&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a nested class in Kotlin?
&lt;/h2&gt;

&lt;p&gt;A nested class is a class declared inside another class. By default, it has no connection to any object of the outer class. The outer class only works like a namespace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderTracker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeliveryStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;etaMinutes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderTracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DeliveryStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Restaurant"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// no OrderTracker needed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A delivery stop makes sense on its own, so you create a &lt;code&gt;DeliveryStop&lt;/code&gt; without any &lt;code&gt;OrderTracker&lt;/code&gt;. Because there is no outer object, the nested class can't read &lt;code&gt;orderId&lt;/code&gt;. If you try, Kotlin 2.4.21 stops you with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Outer class 'class OrderTracker : Any' of non-inner class cannot be used as receiver.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the error that makes many of us reach for &lt;code&gt;inner&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changes when you add inner?
&lt;/h2&gt;

&lt;p&gt;An &lt;a href="https://kotlinlang.org/docs/nested-classes.html#inner-classes" rel="noopener noreferrer"&gt;inner class&lt;/a&gt; belongs to one object of the outer class. It can read and call that object's members, including private ones.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderTracker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;inner&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StatusLabel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Order $orderId"&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;tracker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;@&lt;span class="nc"&gt;OrderTracker&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;tracker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderTracker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"A12"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StatusLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// needs an OrderTracker&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things are different now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You create it from an outer object:&lt;/strong&gt; &lt;code&gt;tracker.StatusLabel()&lt;/code&gt;. Calling &lt;code&gt;OrderTracker.StatusLabel()&lt;/code&gt; fails with "Constructor of the inner class ... can only be called with a receiver of the containing class."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inside it, &lt;code&gt;this&lt;/code&gt; means the inner object.&lt;/strong&gt; To get the outer object, you use a &lt;a href="https://kotlinlang.org/docs/this-expressions.html#qualified-this" rel="noopener noreferrer"&gt;qualified this&lt;/a&gt;: &lt;code&gt;this@OrderTracker&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Java works the other way around. In Java, a class inside a class is inner by default, and you write &lt;code&gt;static&lt;/code&gt; to cut the link. Kotlin makes the safe version the default, and you opt in with &lt;code&gt;inner&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the compiler generate?
&lt;/h2&gt;

&lt;p&gt;You can see this in Android Studio. Open the Kotlin file, go to &lt;strong&gt;Tools &amp;gt; Kotlin &amp;gt; Show Kotlin Bytecode&lt;/strong&gt;, and click &lt;strong&gt;Decompile&lt;/strong&gt;. Here is the shape of the decompiled Java (Kotlin 2.4.21, JVM target 17):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Nested class: static, no link to OrderTracker&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeliveryStop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;etaMinutes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;DeliveryStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;etaMinutes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Inner class: not static, one extra field&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StatusLabel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;OrderTracker&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;$&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// the hidden reference&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;StatusLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderTracker&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;$&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;$&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;$&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The nested class is &lt;code&gt;static&lt;/code&gt;.&lt;/strong&gt; Its constructor takes only its own parameters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The inner class gets a field called &lt;code&gt;this$0&lt;/code&gt;.&lt;/strong&gt; It points to the outer object. It is package-private and marked synthetic (made by the compiler), so you can't see it in Kotlin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The constructor gets an extra first parameter.&lt;/strong&gt; &lt;code&gt;tracker.StatusLabel()&lt;/code&gt; in Kotlin becomes &lt;code&gt;new StatusLabel(tracker)&lt;/code&gt; in bytecode.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;this@OrderTracker&lt;/code&gt; is just a read of &lt;code&gt;this$0&lt;/code&gt;.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more detail. When an inner class uses a private member of the outer class, Kotlin still generates small helper methods in the outer class. For a private property &lt;code&gt;total&lt;/code&gt;, it adds &lt;code&gt;access$getTotal$p&lt;/code&gt;. On Kotlin 2.4.21 you get them with JVM target 17 and with 21, because Kotlin doesn't use Java's newer nest-based access. You can find them in the same Show Kotlin Bytecode window.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can an inner class leak an Activity?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;this$0&lt;/code&gt; field is a strong reference. As long as something holds the inner object, the outer object can't be garbage collected. That's fine when both die together. It's a problem when the inner object is given to something that lives longer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;StatusListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;OrderUpdates&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;            &lt;span class="c1"&gt;// lives as long as the app process&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableListOf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;StatusListener&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusListener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusListener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Almost right: added in onCreate, never removed&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderTrackingActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppCompatActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;lateinit&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TextView&lt;/span&gt;

    &lt;span class="n"&gt;inner&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StatusUpdater&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;OrderUpdates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StatusUpdater&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Right: the listener lives only while the screen is visible&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderTrackingActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppCompatActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;lateinit&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TextView&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;updater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StatusUpdater&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;inner&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StatusUpdater&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nc"&gt;OrderUpdates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updater&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;OrderUpdates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updater&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first version, rotate the phone once. The old Activity is destroyed, but &lt;code&gt;OrderUpdates&lt;/code&gt; still holds the old &lt;code&gt;StatusUpdater&lt;/code&gt;. Its &lt;code&gt;this$0&lt;/code&gt; still points to the old Activity, and the Activity holds its whole view tree. Rotate again and another one stays in memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fklj4wz6kujf0a9adwndx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fklj4wz6kujf0a9adwndx.png" alt="Diagram: how an inner class keeps an Activity alive. OrderUpdates, a singleton, holds the StatusUpdater listener. The listener's hidden this$0 field points to the destroyed OrderTrackingActivity, which holds its views. A nested class has no this$0 arrow." width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the fix. The second version is still an &lt;code&gt;inner&lt;/code&gt; class. What changed is its lifetime: it is removed in &lt;code&gt;onStop&lt;/code&gt;, so nothing outlives the screen. Making the class nested and passing it &lt;code&gt;statusText&lt;/code&gt; would not fix the first version. A View keeps a reference to &lt;a href="https://developer.android.com/reference/android/view/View#getContext()" rel="noopener noreferrer"&gt;its Context&lt;/a&gt;, which here is the Activity. So the real question isn't "inner or nested?" A better question is:&lt;/p&gt;

&lt;p&gt;Does this object live longer than its outer object?&lt;/p&gt;

&lt;p&gt;Android Lint warns about one common case. If you declare a &lt;code&gt;Handler&lt;/code&gt; as an inner class, the &lt;a href="https://googlesamples.github.io/android-custom-lint-rules/checks/HandlerLeak.md.html" rel="noopener noreferrer"&gt;HandlerLeak check&lt;/a&gt; says "This Handler class should be static or leaks might occur". A delayed message on the main thread holds the Handler until it runs, and an inner Handler holds the Activity. In new code, if &lt;code&gt;OrderUpdates&lt;/code&gt; exposed a &lt;code&gt;Flow&lt;/code&gt;, collecting it with &lt;a href="https://developer.android.com/topic/libraries/architecture/coroutines#restart" rel="noopener noreferrer"&gt;&lt;code&gt;repeatOnLifecycle&lt;/code&gt;&lt;/a&gt; would start and stop the collection for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you use inner?
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;inner&lt;/code&gt; when the inner object is a small helper of one outer object and never lives longer than it. A &lt;code&gt;RecyclerView&lt;/code&gt; adapter's ViewHolder that calls the adapter's click handler is a common example. Both are created and dropped together.&lt;/p&gt;

&lt;p&gt;Use a plain nested class for everything else: builders, small helper types, or &lt;a href="https://imayankkumar.com/blog/kotlin-sealed-class/" rel="noopener noreferrer"&gt;sealed class&lt;/a&gt; subtypes grouped under a parent. If the class only needs one value, pass that value in instead of the whole outer object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested vs inner class: quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Nested class&lt;/th&gt;
&lt;th&gt;Inner class&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Keyword&lt;/td&gt;
&lt;td&gt;none (default)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;inner&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Holds the outer object&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, in &lt;code&gt;this$0&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How you create it&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Outer.Nested()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;outer.Inner()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses outer members directly&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, including private&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java equivalent&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;static&lt;/code&gt; nested class&lt;/td&gt;
&lt;td&gt;inner (non-static) class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can keep the outer object alive&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, while it is reachable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Is a Kotlin nested class the same as a Java static nested class?&lt;/strong&gt;&lt;br&gt;
Yes. It compiles to a &lt;code&gt;static&lt;/code&gt; nested class. The &lt;a href="https://imayankkumar.com/blog/kotlin-data-class/" rel="noopener noreferrer"&gt;data class post&lt;/a&gt; also looks at what Kotlin generates for you behind one keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can a nested class read private members of the outer class?&lt;/strong&gt;&lt;br&gt;
Yes, if it has an instance. If &lt;code&gt;OrderTracker&lt;/code&gt; had a &lt;code&gt;private val secret&lt;/code&gt;, a nested class could still read it with &lt;code&gt;fun peek(t: OrderTracker) = t.secret&lt;/code&gt;. What it can't do is read them without an instance, because it has no &lt;code&gt;this$0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does every &lt;code&gt;object :&lt;/code&gt; expression or lambda hold the outer class?&lt;/strong&gt;&lt;br&gt;
No. On Kotlin 2.4.21, an &lt;code&gt;object :&lt;/code&gt; expression or a lambda only stores the outer object if its body uses something from the outer class. One that uses nothing from the outer class gets no &lt;code&gt;this$0&lt;/code&gt;. This is easy to break by accident: one &lt;code&gt;statusText&lt;/code&gt; inside the lambda brings the whole Activity in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;A nested class is a class in a namespace. An inner class is a class with a hidden pointer to its outer object. The pointer is what gives you &lt;code&gt;this@Outer&lt;/code&gt;, and it's also what keeps the outer object alive. Before you give an inner object to something long-lived, check who removes it.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-inner-vs-nested-class/" rel="noopener noreferrer"&gt;inner vs nested class interview questions&lt;/a&gt;. This post is the fifth topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;. The previous one is &lt;a href="https://imayankkumar.com/blog/kotlin-sealed-class/" rel="noopener noreferrer"&gt;sealed classes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Do you have a listener in your app that is added in &lt;code&gt;onCreate&lt;/code&gt; and never removed?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-inner-vs-nested-class/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>When Should You Use a Sealed Class in Kotlin?</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Mon, 16 Nov 2026 02:38:31 +0000</pubDate>
      <link>https://dev.to/imayankkumar/when-should-you-use-a-sealed-class-in-kotlin-3kc</link>
      <guid>https://dev.to/imayankkumar/when-should-you-use-a-sealed-class-in-kotlin-3kc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; a sealed class (or sealed interface) is a type with a closed list of subtypes. The compiler knows all of them, so a &lt;code&gt;when&lt;/code&gt; over it can check that you handled every case. Its direct subtypes must live in the same module and the same package.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Three fields can describe a screen. They can't stop it from being in two states at once.&lt;/p&gt;

&lt;p&gt;Your order screen state starts as three fields: &lt;code&gt;isLoading&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt; and &lt;code&gt;order&lt;/code&gt;. One day a bug report shows a spinner and an error message on the screen at the same time. Nothing in the types stopped &lt;code&gt;isLoading = true&lt;/code&gt; and &lt;code&gt;error = "Timeout"&lt;/code&gt; from being set together.&lt;/p&gt;

&lt;p&gt;A sealed type fixes this kind of bug by making the wrong mix impossible to write. This post covers what a sealed class is and who can extend it. It also shows when to pick a sealed class or a sealed interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a sealed class in Kotlin?
&lt;/h2&gt;

&lt;p&gt;A sealed class is a class with a fixed list of subclasses. You write the subclasses, and nobody else can add more. A sealed interface is the same idea for an interface.&lt;/p&gt;

&lt;p&gt;Think of it this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A regular &lt;code&gt;open&lt;/code&gt; class:&lt;/strong&gt; anyone, in any module, can add a new subclass later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A sealed class:&lt;/strong&gt; the list of subclasses is fixed when your module compiles. Nobody outside can add to it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The screen is in exactly one of these states. &lt;code&gt;Loading&lt;/code&gt; has no data, &lt;code&gt;Ready&lt;/code&gt; has the items, &lt;code&gt;Failed&lt;/code&gt; has the message. You can't build a state that is loading and failed at the same time, because no such type exists.&lt;/p&gt;

&lt;p&gt;A few rules from the &lt;a href="https://kotlinlang.org/docs/sealed-classes.html#constructors" rel="noopener noreferrer"&gt;Kotlin docs&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A sealed class is abstract. You can create its subclasses, never the sealed class itself.&lt;/li&gt;
&lt;li&gt;Its constructors can only be &lt;code&gt;protected&lt;/code&gt; (the default) or &lt;code&gt;private&lt;/code&gt;. A &lt;code&gt;public&lt;/code&gt; one fails with "Constructor must be private or protected in sealed class".&lt;/li&gt;
&lt;li&gt;A subclass can be a &lt;a href="https://imayankkumar.com/blog/kotlin-data-class/" rel="noopener noreferrer"&gt;&lt;code&gt;data class&lt;/code&gt;&lt;/a&gt;, a &lt;code&gt;data object&lt;/code&gt;, a plain class, or another sealed class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why doesn't when need an else branch?
&lt;/h2&gt;

&lt;p&gt;This is the main reason to use a sealed type. The compiler knows every subtype, so it checks that your &lt;code&gt;when&lt;/code&gt; covers all of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Loading"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ready&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Your order (${state.items.size} items)"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Something went wrong"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;is OrderUiState.Ready -&amp;gt;&lt;/code&gt;, Kotlin already knows &lt;code&gt;state&lt;/code&gt; is &lt;code&gt;Ready&lt;/code&gt;, so &lt;code&gt;state.items&lt;/code&gt; works without a cast. This is the same &lt;a href="https://imayankkumar.com/blog/kotlin-null-safety/" rel="noopener noreferrer"&gt;smart cast&lt;/a&gt; you get after a null check.&lt;/p&gt;

&lt;p&gt;Now add a fourth state, &lt;code&gt;Cancelled&lt;/code&gt;. Every &lt;code&gt;when&lt;/code&gt; that doesn't handle it stops compiling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'when' expression must be exhaustive. Add the 'is Cancelled' branch or an 'else' branch.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler gives you a to-do list of every place that needs a decision. This also works for a &lt;code&gt;when&lt;/code&gt; statement, not only for a &lt;code&gt;when&lt;/code&gt; whose result you use. It became an error in Kotlin 1.7, as &lt;a href="https://kotlinlang.org/docs/whatsnew16.html#stable-exhaustive-when-statements-for-enum-sealed-and-boolean-subjects" rel="noopener noreferrer"&gt;planned in the 1.6 notes&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is else almost right?
&lt;/h2&gt;

&lt;p&gt;Have you ever added a new state and found a screen that silently ignored it? The error message above offers an &lt;code&gt;else&lt;/code&gt; branch as a fix. It works, but it switches off the check you wanted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right: compiles today, hides tomorrow's state&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ready&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Your order"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Something went wrong"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Loading"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Right: every state by name, no else&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Loading"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ready&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Your order"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OrderUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Something went wrong"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both versions work with three states. Add &lt;code&gt;data class Cancelled(val reason: String)&lt;/code&gt; and the first one still builds. A cancelled order now shows "Loading" forever, and nothing warns you. The second one fails to build until you decide what a cancelled order should show.&lt;/p&gt;

&lt;p&gt;Keep &lt;code&gt;else&lt;/code&gt; for subjects that are really open, like a &lt;code&gt;String&lt;/code&gt; or an &lt;code&gt;Int&lt;/code&gt;. On a sealed type, list the cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where can the subclasses of a sealed class live?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/sealed-classes.html#inheritance" rel="noopener noreferrer"&gt;The rule&lt;/a&gt;: direct subclasses must be in the &lt;strong&gt;same module&lt;/strong&gt; and the &lt;strong&gt;same package&lt;/strong&gt; as the sealed type. They can be top-level or nested, in any file. On Kotlin 2.4.21 the compiler reports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Same package, another file, same module:&lt;/strong&gt; allowed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Another package, same module:&lt;/strong&gt; "A class can only extend a sealed class or interface declared in the same package."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Another module, even with the same package name:&lt;/strong&gt; "Extending sealed classes or interfaces from a different module is prohibited."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A local class or an anonymous object:&lt;/strong&gt; "Local class cannot extend a sealed interface." Subclasses need a proper name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqjt2p0g7dssoa81fd4uu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqjt2p0g7dssoa81fd4uu.png" alt="Diagram: who can extend a sealed type. Inside module core:model, package com.food.order, the direct subtypes Loading, Ready and Failed are allowed in any file. Another package in the same module is blocked. Another module is blocked, even with the same package name. An indirect subclass of an open subclass is allowed anywhere." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a multi-module Android app this matters. If &lt;code&gt;OrderUiState&lt;/code&gt; lives in &lt;code&gt;:core:model&lt;/code&gt;, all its direct states must live there too. A &lt;code&gt;:feature:checkout&lt;/code&gt; module can use them and &lt;code&gt;when&lt;/code&gt; over them, but can't add a new one. That is also why a library can expose a sealed type safely: apps read it, but can't extend it.&lt;/p&gt;

&lt;p&gt;Older answers often say "same file". That was the rule before &lt;a href="https://kotlinlang.org/docs/whatsnew15.html#package-wide-sealed-class-hierarchies" rel="noopener noreferrer"&gt;Kotlin 1.5&lt;/a&gt;, which moved it to package and module and also added &lt;a href="https://kotlinlang.org/docs/whatsnew15.html#sealed-interfaces" rel="noopener noreferrer"&gt;sealed interfaces&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about indirect subclasses?
&lt;/h2&gt;

&lt;p&gt;The rules above apply only to &lt;strong&gt;direct&lt;/strong&gt; subclasses. If a direct subclass is &lt;code&gt;open&lt;/code&gt; instead of sealed, anyone can extend it, even from another module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Paid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;receiptUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Declined&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;open&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In another module: allowed, because Pending is open&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UpiPending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;when&lt;/code&gt; on &lt;code&gt;PaymentResult&lt;/code&gt; is still exhaustive. The &lt;code&gt;is PaymentResult.Pending&lt;/code&gt; branch also covers &lt;code&gt;UpiPending&lt;/code&gt;, because a &lt;code&gt;UpiPending&lt;/code&gt; is a &lt;code&gt;Pending&lt;/code&gt;. What you lose is a closed list of the kinds of pending. If you want the list closed all the way down, make &lt;code&gt;Pending&lt;/code&gt; sealed too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sealed class or sealed interface?
&lt;/h2&gt;

&lt;p&gt;Both give you a closed list and an exhaustive &lt;code&gt;when&lt;/code&gt;. The difference is what you can put in the parent and how many parents a type can have.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sealed class:&lt;/strong&gt; can have a constructor and state. In &lt;code&gt;PaymentResult&lt;/code&gt; above, every result gets &lt;code&gt;orderId&lt;/code&gt; from the parent. A class can extend only one class, so a type belongs to one sealed class hierarchy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sealed interface:&lt;/strong&gt; no constructor or stored state. A class can implement many interfaces, so one type can be part of two closed lists. An &lt;code&gt;enum&lt;/code&gt; can implement a sealed interface, but can't extend a sealed class.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CartEvent&lt;/span&gt;
&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AnalyticsEvent&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ItemAdded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CartEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AnalyticsEvent&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SortOrder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CartEvent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;PRICE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RATING&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ItemAdded&lt;/code&gt; is in both lists. The same-module and same-package rule still applies to each interface, so &lt;code&gt;ItemAdded&lt;/code&gt; has to live next to both of them.&lt;/p&gt;

&lt;p&gt;A simple default: use a sealed interface for UI state and events. Use a sealed class when every subtype needs the same constructor data, like &lt;code&gt;orderId&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sealed class vs sealed interface: quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Sealed class&lt;/th&gt;
&lt;th&gt;Sealed interface&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Closed list of subtypes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exhaustive &lt;code&gt;when&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Constructor and stored state&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A type can be in two lists&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;An &lt;code&gt;enum&lt;/code&gt; can join&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, by implementing it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where subtypes can live&lt;/td&gt;
&lt;td&gt;Same module and package&lt;/td&gt;
&lt;td&gt;Same module and package&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Good for&lt;/td&gt;
&lt;td&gt;Results with shared fields, like &lt;code&gt;orderId&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;UI state, events&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can a data class extend a sealed class?&lt;/strong&gt;&lt;br&gt;
Yes. A data class can't be sealed itself. But it can extend a sealed class or implement a sealed interface. Most UI states are written this way. The &lt;a href="https://imayankkumar.com/blog/kotlin-data-class/" rel="noopener noreferrer"&gt;data class post&lt;/a&gt; explains what &lt;code&gt;data&lt;/code&gt; adds, and why &lt;code&gt;data object&lt;/code&gt; prints a readable name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is adding a new subclass a breaking change?&lt;/strong&gt;&lt;br&gt;
For every &lt;code&gt;when&lt;/code&gt; without &lt;code&gt;else&lt;/code&gt;, yes, and that is the point. For a public library it means callers must update their code, so add new cases with care.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should I use a sealed class or an enum?&lt;/strong&gt;&lt;br&gt;
Use an enum when every case is a single fixed value with the same properties, like &lt;code&gt;SortOrder.PRICE&lt;/code&gt;. Use a sealed type when cases carry different data, like &lt;code&gt;Failed("Timeout")&lt;/code&gt;. I'll cover enums in detail in a later post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How is this different from sealed classes in Java?&lt;/strong&gt;&lt;br&gt;
Java added sealed classes in &lt;a href="https://openjdk.org/jeps/409" rel="noopener noreferrer"&gt;Java 17&lt;/a&gt;, where you list the allowed subclasses with &lt;code&gt;permits&lt;/code&gt;. Kotlin has no &lt;code&gt;permits&lt;/code&gt; list: the compiler finds the subclasses in the same package and module.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;A sealed type turns "all the states this screen can be in" into a list the compiler checks for you. Direct subtypes stay in one module and one package. Indirect ones can live anywhere. And an &lt;code&gt;else&lt;/code&gt; on a sealed &lt;code&gt;when&lt;/code&gt; throws away the check.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-sealed-class/" rel="noopener noreferrer"&gt;sealed class interview questions&lt;/a&gt;. This post is the fourth topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Is there an &lt;code&gt;else -&amp;gt;&lt;/code&gt; on a sealed type in your codebase right now?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-sealed-class/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kotlin Data Class vs Regular Class Explained Simply</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Sat, 14 Nov 2026 13:27:25 +0000</pubDate>
      <link>https://dev.to/imayankkumar/kotlin-data-class-vs-regular-class-explained-simply-170k</link>
      <guid>https://dev.to/imayankkumar/kotlin-data-class-vs-regular-class-explained-simply-170k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; a data class is a normal class where the compiler writes five things for you: &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt;, &lt;code&gt;toString()&lt;/code&gt;, &lt;code&gt;copy()&lt;/code&gt; and &lt;code&gt;component1()&lt;/code&gt;, &lt;code&gt;component2()&lt;/code&gt; and so on. It uses only the properties in the primary constructor, and &lt;code&gt;copy()&lt;/code&gt; copies values, not the objects inside them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You add an item to the cart, the ViewModel updates its &lt;code&gt;StateFlow&lt;/code&gt;, and the screen does not change. No crash, nothing in Logcat. The state is a data class and the update uses &lt;code&gt;copy()&lt;/code&gt;. The bug is in how &lt;code&gt;copy()&lt;/code&gt; and &lt;code&gt;equals()&lt;/code&gt; work together.&lt;/p&gt;

&lt;p&gt;This post covers what the &lt;code&gt;data&lt;/code&gt; keyword adds, the rules it comes with, and the two details behind surprising data class bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a data class in Kotlin?
&lt;/h2&gt;

&lt;p&gt;A data class is a class whose main job is to hold values. You write it in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference shows up when you compare it with the same class without &lt;code&gt;data&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlainCartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paneer Roll"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paneer Roll"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PlainCartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paneer Roll"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PlainCartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paneer Roll"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;   &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A regular class gets &lt;code&gt;equals()&lt;/code&gt; from &lt;a href="https://imayankkumar.com/blog/kotlin-any-unit-nothing/" rel="noopener noreferrer"&gt;&lt;code&gt;Any&lt;/code&gt;&lt;/a&gt;, which only asks "is this the same object in memory?". A data class compares the values instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the compiler generate for a data class?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/data-classes.html" rel="noopener noreferrer"&gt;The Kotlin docs list five things&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;equals()&lt;/code&gt;&lt;/strong&gt;: two objects are equal when all constructor properties are equal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;hashCode()&lt;/code&gt;&lt;/strong&gt;: built from the same properties, so equal objects get the same hash. That is why a data class works as a &lt;code&gt;HashMap&lt;/code&gt; key. Put the two &lt;code&gt;CartItem&lt;/code&gt;s above in &lt;code&gt;setOf()&lt;/code&gt; and you get one item; with &lt;code&gt;PlainCartItem&lt;/code&gt; you get two.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/strong&gt;: &lt;code&gt;CartItem(name=Paneer Roll, price=120, quantity=2)&lt;/code&gt; instead of &lt;code&gt;PlainCartItem@7a81197d&lt;/code&gt;. Very useful in logs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;componentN()&lt;/code&gt;&lt;/strong&gt;: &lt;code&gt;component1()&lt;/code&gt; returns &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;component2()&lt;/code&gt; returns &lt;code&gt;price&lt;/code&gt;, in declaration order. This makes &lt;code&gt;val (name, price, quantity) = item&lt;/code&gt; work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;copy()&lt;/code&gt;&lt;/strong&gt;: a new object with the same values, where you can change some: &lt;code&gt;item.copy(quantity = 3)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Facnrkqjl4lf4xngj8746.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Facnrkqjl4lf4xngj8746.png" alt="Diagram: one line, data class CartItem, turns into five generated functions: equals, hashCode, toString, component1 to 3, and copy. Two warnings: only constructor properties count, and copy() is shallow." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the rules for a data class?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/data-classes.html" rel="noopener noreferrer"&gt;The compiler checks four rules&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The primary constructor needs at least one parameter.&lt;/li&gt;
&lt;li&gt;Every parameter in it must be &lt;code&gt;val&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A data class can't be &lt;code&gt;abstract&lt;/code&gt;, &lt;code&gt;open&lt;/code&gt;, &lt;code&gt;sealed&lt;/code&gt; or &lt;code&gt;inner&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can't write your own &lt;code&gt;copy()&lt;/code&gt; or &lt;code&gt;componentN()&lt;/code&gt; with the same parameters the compiler would generate. You can write your own &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; or &lt;code&gt;toString()&lt;/code&gt;, and then the compiler uses yours.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rule 3 means nobody can extend a data class. It can still implement interfaces, which is why &lt;code&gt;data class Ready(...) : UiState&lt;/code&gt; is so common in Android code. Break a rule and you get a compile error, like "Modifier 'open' is incompatible with 'data'".&lt;/p&gt;

&lt;h2&gt;
  
  
  Which properties count in equals()?
&lt;/h2&gt;

&lt;p&gt;Only the ones inside the primary constructor. A property declared in the class body is ignored by all five generated functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isSeen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two &lt;code&gt;Order&lt;/code&gt;s with the same &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;items&lt;/code&gt; are equal, even if one has &lt;code&gt;isSeen = true&lt;/code&gt;. &lt;code&gt;toString()&lt;/code&gt; doesn't show it, and &lt;code&gt;copy()&lt;/code&gt; doesn't carry it over: the copy starts with &lt;code&gt;false&lt;/code&gt; again. If a value matters, put it in the constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is copy() a deep copy?
&lt;/h2&gt;

&lt;p&gt;No. &lt;a href="https://kotlinlang.org/docs/data-classes.html#copying" rel="noopener noreferrer"&gt;&lt;code&gt;copy()&lt;/code&gt; is shallow&lt;/a&gt;: it copies each property's value, and for an object that value is a reference. So the copy and the original point to the same list.&lt;/p&gt;

&lt;p&gt;This is the bug from the start of the post:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right: the state holds a MutableList&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CartState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MutableList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CartState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mutableListOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// in the ViewModel&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// changes the list that old points to&lt;/span&gt;
&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// new object, same list, so new == old&lt;/span&gt;

&lt;span class="c1"&gt;// Right: a read-only List, and a new list for every change&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CartState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CartState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;state&lt;/code&gt; is the &lt;code&gt;StateFlow&lt;/code&gt; the cart screen collects. In the first version, &lt;code&gt;add()&lt;/code&gt; changes the list inside the old state. &lt;code&gt;copy()&lt;/code&gt; then makes a new &lt;code&gt;CartState&lt;/code&gt; pointing to that same list, so the new state equals the old one. &lt;a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/" rel="noopener noreferrer"&gt;&lt;code&gt;StateFlow&lt;/code&gt; skips a value equal to the current one&lt;/a&gt;, so the screen never hears about it. You can test this yourself: collect &lt;code&gt;state&lt;/code&gt; and print every value. The first version prints only the starting state. The second also prints the update.&lt;/p&gt;

&lt;p&gt;In the second version, &lt;code&gt;it.items + item&lt;/code&gt; creates a new list, so the new state is really different and &lt;code&gt;StateFlow&lt;/code&gt; sends it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does copy() warn about a private constructor?
&lt;/h2&gt;

&lt;p&gt;A private constructor is a common way to check values first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coupon&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Coupon&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percent&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Coupon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;coupon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Coupon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;tooBig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coupon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// skips the 1..50 check&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated &lt;code&gt;copy()&lt;/code&gt; is public, so anyone can make a &lt;code&gt;Coupon(90)&lt;/code&gt;. &lt;a href="https://kotlinlang.org/docs/whatsnew2020.html#data-class-copy-function-to-have-the-same-visibility-as-constructor" rel="noopener noreferrer"&gt;Since Kotlin 2.0.20&lt;/a&gt; the compiler warns about this, and the plan is to give &lt;code&gt;copy()&lt;/code&gt; the same visibility as the constructor. You can choose early:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@ConsistentCopyVisibility&lt;/code&gt; makes &lt;code&gt;copy()&lt;/code&gt; private now. The last line above then fails with a "Cannot access ... it is private" error.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@ExposedCopyVisibility&lt;/code&gt; keeps the old public &lt;code&gt;copy()&lt;/code&gt; and hides the warning on the class. Callers still get warned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As of Kotlin 2.4.21 (November 2026), it is still a warning. &lt;a href="https://youtrack.jetbrains.com/issue/KT-11914" rel="noopener noreferrer"&gt;KT-11914&lt;/a&gt; tracks when that changes. If your class has a private constructor, add &lt;code&gt;@ConsistentCopyVisibility&lt;/code&gt; today.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a data class look like in bytecode?
&lt;/h2&gt;

&lt;p&gt;Compile &lt;code&gt;CartItem&lt;/code&gt; with Kotlin 2.4.21 and run &lt;code&gt;javap&lt;/code&gt; on it. Without method bodies, the one line becomes (shortened):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// + getPrice(), getQuantity()&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;component1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// + component2(), component3()&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt; &lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;CartItem&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;$&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CartItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class is &lt;code&gt;final&lt;/code&gt;: that is rule 3 in bytecode. &lt;code&gt;copy$default&lt;/code&gt; handles default arguments: an &lt;code&gt;int&lt;/code&gt; mask marks which ones you passed, and the rest come from the old object. And &lt;code&gt;hashCode()&lt;/code&gt; is the classic &lt;code&gt;result * 31 + next&lt;/code&gt; over the three fields, the same code you would write by hand in Java. To see it yourself in Android Studio: &lt;em&gt;Tools &amp;gt; Kotlin &amp;gt; Show Kotlin Bytecode&lt;/em&gt;, then &lt;em&gt;Decompile&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data class vs regular class: quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Regular class&lt;/th&gt;
&lt;th&gt;Data class&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;==&lt;/code&gt; compares&lt;/td&gt;
&lt;td&gt;Same object in memory&lt;/td&gt;
&lt;td&gt;Constructor property values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PlainCartItem@7a81197d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CartItem(name=..., price=...)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;copy()&lt;/code&gt; and destructuring&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be &lt;code&gt;open&lt;/code&gt; or &lt;code&gt;abstract&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Constructor&lt;/td&gt;
&lt;td&gt;Anything, or none&lt;/td&gt;
&lt;td&gt;At least one &lt;code&gt;val&lt;/code&gt;/&lt;code&gt;var&lt;/code&gt; parameter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Good for&lt;/td&gt;
&lt;td&gt;Behavior: ViewModels, repositories, managers&lt;/td&gt;
&lt;td&gt;Values: UI state, API models, events with data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can a data class have a property that is not in the constructor?&lt;/strong&gt;&lt;br&gt;
Yes, but the generated functions ignore it. Use it only for values that should not affect equality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should data class properties be &lt;code&gt;val&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
Prefer &lt;code&gt;val&lt;/code&gt;. With &lt;code&gt;var&lt;/code&gt;, an object can change after you put it in a &lt;code&gt;HashSet&lt;/code&gt;. Its hash changes too, so the set can no longer find it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a &lt;code&gt;data object&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://kotlinlang.org/docs/object-declarations.html#data-objects" rel="noopener noreferrer"&gt;An &lt;code&gt;object&lt;/code&gt; with a readable &lt;code&gt;toString()&lt;/code&gt;&lt;/a&gt;: it prints &lt;code&gt;Loading&lt;/code&gt;, not &lt;code&gt;Loading@1b6d3586&lt;/code&gt;. Use it for states with no data, like &lt;code&gt;data object Loading : UiState&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;data&lt;/code&gt; keyword is a shortcut, not a different kind of class. It writes five functions from your constructor properties, and nothing more. Many data class bugs come from forgetting the "nothing more": body properties are ignored, and &lt;code&gt;copy()&lt;/code&gt; shares the objects inside.&lt;/p&gt;

&lt;p&gt;The practical habit: build state classes from &lt;code&gt;val&lt;/code&gt;s and read-only types like &lt;code&gt;List&lt;/code&gt;, and make changes with &lt;code&gt;copy()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-data-class/" rel="noopener noreferrer"&gt;data class interview questions&lt;/a&gt;. This post is the third topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Have you ever had a &lt;code&gt;StateFlow&lt;/code&gt; that didn't update even though the code called &lt;code&gt;copy()&lt;/code&gt;?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-data-class/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kotlin Any, Unit and Nothing Explained Simply</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Mon, 09 Nov 2026 13:05:43 +0000</pubDate>
      <link>https://dev.to/imayankkumar/kotlin-any-unit-and-nothing-explained-simply-4kjf</link>
      <guid>https://dev.to/imayankkumar/kotlin-any-unit-and-nothing-explained-simply-4kjf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; &lt;code&gt;Any&lt;/code&gt; is the parent of every non-null type, so it can hold any value. &lt;code&gt;Unit&lt;/code&gt; means "the work is done, there is no result to give back". &lt;code&gt;Nothing&lt;/code&gt; means "this code never finishes normally", for example because it always throws.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Open a typical Android screen written in Kotlin and you will often find all three. A button takes &lt;code&gt;onClick: () -&amp;gt; Unit&lt;/code&gt;. A loading state is &lt;code&gt;UiState&amp;lt;Nothing&amp;gt;&lt;/code&gt;. An analytics call takes values of type &lt;code&gt;Any?&lt;/code&gt;. They look like small details, but each one tells the compiler something useful.&lt;/p&gt;

&lt;p&gt;This post covers what each type means, how it differs from Java, and where you use it in real Android code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where do Any, Unit and Nothing fit in Kotlin?
&lt;/h2&gt;

&lt;p&gt;Think of Kotlin's types as a ladder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At the top is &lt;code&gt;Any?&lt;/code&gt;. Every value fits here, even &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Just below is &lt;code&gt;Any&lt;/code&gt;. Every value except &lt;code&gt;null&lt;/code&gt; fits here.&lt;/li&gt;
&lt;li&gt;In the middle are the types you use every day: built-in ones like &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Int&lt;/code&gt;, your own classes like &lt;code&gt;Order&lt;/code&gt; (from the food-order app in this series), and also &lt;code&gt;Unit&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At the bottom is &lt;code&gt;Nothing&lt;/code&gt;. No value fits here at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A type high on the ladder accepts more values but tells you less about them. A type low on the ladder tells you more. &lt;code&gt;Nothing&lt;/code&gt; is the extreme case: it tells the compiler "you will never get a value from here".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjxbyn22jos7rdtdmwbtg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjxbyn22jos7rdtdmwbtg.png" alt="Diagram: Kotlin's type ladder. Any? at the top accepts every value including null. Any accepts every non-null value. String, Int, Order and Unit sit in the middle. Nothing is at the bottom and has no values." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Any in Kotlin?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/inheritance.html" rel="noopener noreferrer"&gt;Every Kotlin class has &lt;code&gt;Any&lt;/code&gt; as its parent&lt;/a&gt;, even if you don't write it. That is why every object has three functions: &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;toString()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So a parameter of type &lt;code&gt;Any&lt;/code&gt; accepts anything that is not null. A common Android place for this is analytics, where one event can carry a number, a text and an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Sends one analytics event with any mix of values&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;logEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vararg&lt;/span&gt; &lt;span class="k"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;logEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"checkout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"items"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"coupon"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="s2"&gt;"SAVE10"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"note"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here each value is &lt;code&gt;Any?&lt;/code&gt;, because the note can be missing. To use a value as its real type, you check it with &lt;code&gt;is&lt;/code&gt;. After &lt;code&gt;if (value is String)&lt;/code&gt;, the compiler lets you call &lt;code&gt;value.length&lt;/code&gt;. This is the same smart cast you saw in the &lt;a href="https://imayankkumar.com/blog/kotlin-null-safety/" rel="noopener noreferrer"&gt;null safety post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On the JVM, Kotlin &lt;a href="https://kotlinlang.org/docs/java-interop.html#mapped-types" rel="noopener noreferrer"&gt;maps Java's &lt;code&gt;Object&lt;/code&gt; to &lt;code&gt;Any&lt;/code&gt;&lt;/a&gt;. They are not exactly the same, though. &lt;code&gt;Any&lt;/code&gt; only has those three functions, so &lt;code&gt;wait()&lt;/code&gt; and &lt;code&gt;notify()&lt;/code&gt; are not available on it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Any&lt;/code&gt; is useful at the edges of your app: logging, analytics, a &lt;code&gt;Bundle&lt;/code&gt;. Inside your own code, it throws away type information. If a function takes &lt;code&gt;Any&lt;/code&gt; and then checks five types with &lt;code&gt;is&lt;/code&gt;, a sealed type or a generic is usually a better fit.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Unit, and how is it different from void?
&lt;/h2&gt;

&lt;p&gt;A function that does some work but has no result returns &lt;code&gt;Unit&lt;/code&gt;. You usually don't write it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;clearCart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cartItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// return type is Unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Java, &lt;code&gt;void&lt;/code&gt; means "no value at all". &lt;a href="https://kotlinlang.org/docs/functions.html#unit-returning-functions" rel="noopener noreferrer"&gt;In Kotlin, &lt;code&gt;Unit&lt;/code&gt; is a real type with exactly one value&lt;/a&gt;, the object &lt;code&gt;Unit&lt;/code&gt;. You can even store it: &lt;code&gt;val result = clearCart()&lt;/code&gt; compiles, and printing it shows &lt;code&gt;kotlin.Unit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You might worry this costs extra. It doesn't. Compile &lt;code&gt;clearCart()&lt;/code&gt; with Kotlin 2.4.0 and look at the bytecode: it is a normal &lt;code&gt;void&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;So why have a real type? Because it works with generics, and &lt;code&gt;void&lt;/code&gt; does not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;onClick: () -&amp;gt; Unit&lt;/code&gt; is a function type that returns nothing useful.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;suspend fun cancelOrder(id: String): Result&amp;lt;Unit&amp;gt;&lt;/code&gt; says "it worked or it failed, there is no data".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MutableSharedFlow&amp;lt;Unit&amp;gt;&lt;/code&gt; sends a one-time event from a ViewModel, like "the order was placed, close the cart screen". The event has no data. The fact that it happened is the message, so the type is &lt;code&gt;Unit&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Java you need the &lt;code&gt;Void&lt;/code&gt; class and &lt;code&gt;return null&lt;/code&gt; for this. In Kotlin, &lt;code&gt;Unit&lt;/code&gt; just fits.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Nothing, and why would a function return it?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/exceptions.html#the-nothing-type" rel="noopener noreferrer"&gt;&lt;code&gt;Nothing&lt;/code&gt; is the type of code that never finishes normally&lt;/a&gt;. It has no values, so a function that returns &lt;code&gt;Nothing&lt;/code&gt; can only throw or loop forever.&lt;/p&gt;

&lt;p&gt;You already use it. &lt;code&gt;throw&lt;/code&gt; is an expression of type &lt;code&gt;Nothing&lt;/code&gt;. The standard library's &lt;a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/error.html" rel="noopener noreferrer"&gt;&lt;code&gt;error()&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-t-o-d-o.html" rel="noopener noreferrer"&gt;&lt;code&gt;TODO()&lt;/code&gt;&lt;/a&gt; both return &lt;code&gt;Nothing&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;Nothing&lt;/code&gt; is at the bottom of the ladder, it fits wherever any type is expected. That is why this compiles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;findOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;// null when not found&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Order $orderId not loaded"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;?:&lt;/code&gt; has two paths. If &lt;code&gt;findOrder&lt;/code&gt; finds the order, you get it. If it returns null, &lt;code&gt;error()&lt;/code&gt; throws, so that path never gives a value: its type is &lt;code&gt;Nothing&lt;/code&gt;. &lt;code&gt;Nothing&lt;/code&gt; fits into any type, so the whole line is an &lt;code&gt;Order&lt;/code&gt;, not &lt;code&gt;Order?&lt;/code&gt;. That is why &lt;code&gt;order&lt;/code&gt; needs no &lt;code&gt;?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The return type matters when you write your own helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right: the helper always throws, but the compiler doesn't know&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;stopOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Order $id not loaded"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;orderTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;stopOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;   &lt;span class="c1"&gt;// error: order is still Order?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Right: Nothing tells the compiler this call never comes back&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;stopOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Nothing&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Order $id not loaded"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Now order.total compiles, because order can't be null after that line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first version, &lt;code&gt;stopOrder&lt;/code&gt; returns &lt;code&gt;Unit&lt;/code&gt;. The compiler thinks the code can continue after it, with &lt;code&gt;order&lt;/code&gt; still null. So you get "Only safe (?.) or non-null asserted (!!.) calls are allowed", and the quick fix is &lt;code&gt;order!!&lt;/code&gt;. With &lt;code&gt;: Nothing&lt;/code&gt;, the compiler knows the code after that line only runs when &lt;code&gt;order&lt;/code&gt; is not null. So it changes the type of &lt;code&gt;order&lt;/code&gt; from &lt;code&gt;Order?&lt;/code&gt; to &lt;code&gt;Order&lt;/code&gt; for you. No &lt;code&gt;!!&lt;/code&gt; needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why can a loading state be &lt;code&gt;UiState&amp;lt;Nothing&amp;gt;&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;This is a common place to see &lt;code&gt;Nothing&lt;/code&gt; in Android code. Many apps call this class &lt;code&gt;UiState&lt;/code&gt;. Here it is &lt;code&gt;OrderState&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ready&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;   &lt;span class="c1"&gt;// compiles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Loading&lt;/code&gt; has no data, so its type argument is &lt;code&gt;Nothing&lt;/code&gt;. Because &lt;code&gt;Nothing&lt;/code&gt; is a subtype of &lt;code&gt;Order&lt;/code&gt;, and &lt;code&gt;T&lt;/code&gt; is marked &lt;a href="https://kotlinlang.org/docs/generics.html#declaration-site-variance" rel="noopener noreferrer"&gt;&lt;code&gt;out&lt;/code&gt;&lt;/a&gt;, &lt;code&gt;OrderState&amp;lt;Nothing&amp;gt;&lt;/code&gt; is a subtype of &lt;code&gt;OrderState&amp;lt;Order&amp;gt;&lt;/code&gt;. So one &lt;code&gt;Loading&lt;/code&gt; object works for every &lt;code&gt;OrderState&lt;/code&gt;, whatever its &lt;code&gt;T&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;Remove the &lt;code&gt;out&lt;/code&gt; and the last line fails with "initializer type mismatch". The &lt;code&gt;out&lt;/code&gt; is what makes the trick work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Any vs Unit vs Nothing: quick comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;How many values&lt;/th&gt;
&lt;th&gt;Where you see it on Android&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Any?&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Anything, or null&lt;/td&gt;
&lt;td&gt;All values&lt;/td&gt;
&lt;td&gt;Analytics params, &lt;code&gt;Map&amp;lt;String, Any?&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Any&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Anything except null&lt;/td&gt;
&lt;td&gt;All non-null values&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;equals(other: Any?)&lt;/code&gt;, logging helpers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Unit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Done, no result&lt;/td&gt;
&lt;td&gt;One: &lt;code&gt;Unit&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;() -&amp;gt; Unit&lt;/code&gt;, &lt;code&gt;Result&amp;lt;Unit&amp;gt;&lt;/code&gt;, &lt;code&gt;Flow&amp;lt;Unit&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Nothing&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Never finishes normally&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;throw&lt;/code&gt;, &lt;code&gt;error()&lt;/code&gt;, &lt;code&gt;TODO()&lt;/code&gt;, &lt;code&gt;UiState&amp;lt;Nothing&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Is &lt;code&gt;Any&lt;/code&gt; the same as Java's &lt;code&gt;Object&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
On the JVM they map to each other, so a Java method that takes &lt;code&gt;Object&lt;/code&gt; takes &lt;code&gt;Any!&lt;/code&gt; in Kotlin. But &lt;code&gt;Any&lt;/code&gt; is a Kotlin type with only three functions, and it exists on every Kotlin platform, not only the JVM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the difference between &lt;code&gt;Unit&lt;/code&gt; and &lt;code&gt;Nothing&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
A &lt;code&gt;Unit&lt;/code&gt; function finishes and comes back, just without a useful result. A &lt;code&gt;Nothing&lt;/code&gt; function never comes back. In bytecode, &lt;code&gt;Unit&lt;/code&gt; functions become &lt;code&gt;void&lt;/code&gt;, while a &lt;code&gt;Nothing&lt;/code&gt; function shows up as returning &lt;code&gt;java.lang.Void&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the type of &lt;code&gt;null&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;val x = null&lt;/code&gt; gives &lt;code&gt;x&lt;/code&gt; the type &lt;code&gt;Nothing?&lt;/code&gt;. The only value of &lt;code&gt;Nothing?&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;, and since &lt;code&gt;Nothing?&lt;/code&gt; sits under every nullable type, &lt;code&gt;null&lt;/code&gt; fits into any &lt;code&gt;T?&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Any&lt;/code&gt; is the top of the ladder: it accepts everything and tells you little. &lt;code&gt;Unit&lt;/code&gt; is a real value that means "done". &lt;code&gt;Nothing&lt;/code&gt; is the bottom: it tells the compiler a path never comes back, and the compiler uses that to make your other types more exact.&lt;/p&gt;

&lt;p&gt;The practical habit: when you write a helper that always throws, give it &lt;code&gt;: Nothing&lt;/code&gt;. Your smart casts will start working, and a few &lt;code&gt;!!&lt;/code&gt; will go away.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-any-unit-nothing/" rel="noopener noreferrer"&gt;Any, Unit and Nothing interview questions&lt;/a&gt;. This post is the second topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Have you seen &lt;code&gt;UiState&amp;lt;Nothing&amp;gt;&lt;/code&gt; in your codebase, or does your team write a separate state class for every screen?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-any-unit-nothing/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kotlin Null Safety Explained Simply: ?, ?., ?:, !! and Platform Types</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Tue, 03 Nov 2026 19:39:32 +0000</pubDate>
      <link>https://dev.to/imayankkumar/kotlin-null-safety-explained-simply-and-platform-types-21d3</link>
      <guid>https://dev.to/imayankkumar/kotlin-null-safety-explained-simply-and-platform-types-21d3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt; In Kotlin, a type says if a value can be null. &lt;code&gt;String&lt;/code&gt; can never be null. &lt;code&gt;String?&lt;/code&gt; can. You must handle the null case before you use a &lt;code&gt;String?&lt;/code&gt;. So most null crashes become build errors instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every Android developer has seen this line in a crash report: &lt;code&gt;java.lang.NullPointerException&lt;/code&gt;. The app tried to use something that was not there, like an order with no delivery note.&lt;/p&gt;

&lt;p&gt;This post covers how Kotlin's null safety works, the tools you use every day, and where a crash can still get through.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is null, and why does it crash apps?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;null&lt;/code&gt; means "no value". Think of a contact in your phone with no phone number saved. Asking it for a number is fine. Calling that number is the problem.&lt;/p&gt;

&lt;p&gt;In Java, any object can be null, and the compiler does not warn you. You find out at runtime, when the app crashes with a NullPointerException (NPE). Tony Hoare, who added null references to a language in 1965, later called it his &lt;a href="https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/" rel="noopener noreferrer"&gt;"billion-dollar mistake"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The usual Java fix is &lt;code&gt;if (x != null)&lt;/code&gt; everywhere. That works only until someone forgets.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Kotlin stop null crashes?
&lt;/h2&gt;

&lt;p&gt;Kotlin puts the answer into the type itself. Every type comes in &lt;a href="https://kotlinlang.org/docs/null-safety.html#nullable-types-and-non-nullable-types" rel="noopener noreferrer"&gt;two versions&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; is non-nullable. It always holds a real string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String?&lt;/code&gt; is nullable. It holds a string or &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Non-nullable is the default. If you try &lt;code&gt;var restaurant: String = null&lt;/code&gt;, the build fails with "Null cannot be a value of a non-null type 'String'."&lt;/p&gt;

&lt;p&gt;The rule for &lt;code&gt;String?&lt;/code&gt; is just as strict. If &lt;code&gt;deliveryNote&lt;/code&gt; is a &lt;code&gt;String?&lt;/code&gt;, then &lt;code&gt;deliveryNote.length&lt;/code&gt; does not compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Only safe (?.) or non-null asserted (!!.) calls are allowed
on a nullable receiver of type 'String?'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you decide what happens when the value is missing. You decide it while writing the code, not at 2 AM from a crash report.&lt;/p&gt;

&lt;p&gt;You already see this on Android. In &lt;code&gt;onCreate(savedInstanceState: Bundle?)&lt;/code&gt;, the &lt;code&gt;?&lt;/code&gt; tells you the bundle is null on the first launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you use a value that might be null?
&lt;/h2&gt;

&lt;p&gt;There are four everyday tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Check it with &lt;code&gt;if&lt;/code&gt;&lt;/strong&gt; After &lt;code&gt;if (order != null)&lt;/code&gt;, the compiler treats &lt;code&gt;order&lt;/code&gt; as non-null inside that block. This is called a smart cast: the compiler changes the type for you because it saw the check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Safe call &lt;code&gt;?.&lt;/code&gt;&lt;/strong&gt; &lt;code&gt;order?.total&lt;/code&gt; means "give me the total if &lt;code&gt;order&lt;/code&gt; is not null, otherwise give me null". You can chain it: &lt;code&gt;order?.rider?.phone&lt;/code&gt;. If any part is null, the whole result is null and nothing crashes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Elvis operator &lt;code&gt;?:&lt;/code&gt;&lt;/strong&gt; It gives a default when the left side is null: &lt;code&gt;order?.deliveryNote ?: "No note"&lt;/code&gt;. It can also leave the function early: &lt;code&gt;val order = findOrder(orderId) ?: return&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;let&lt;/code&gt; with &lt;code&gt;?.&lt;/code&gt;&lt;/strong&gt; &lt;code&gt;order?.let { show(it) }&lt;/code&gt; runs the block only when &lt;code&gt;order&lt;/code&gt; is not null. Inside the block, &lt;code&gt;it&lt;/code&gt; is non-null.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F13sam8tc7viioq9mxptu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F13sam8tc7viioq9mxptu.png" alt="Diagram: a value of type Order? comes from Kotlin code, or from Java as Order! which you should treat as Order?. It has four safe paths (if check, safe call, Elvis default, let) and one risky path, !!, which crashes when the value is null." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is &lt;code&gt;!!&lt;/code&gt; risky?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;!!&lt;/code&gt; is the not-null assertion operator. It tells the compiler: "Trust me, this is not null." If you are wrong, you get the same NPE that Kotlin was trying to prevent.&lt;/p&gt;

&lt;p&gt;When the compiler shows a red line, adding &lt;code&gt;!!&lt;/code&gt; is the fastest way to make it go away. That is true whether you, a teammate or an AI tool writes the fix. Here is what that looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right: compiles, but crashes when the order is not found&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;orderTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// can be null&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Order total: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;  &lt;span class="c1"&gt;// crashes if null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Right: the "not found" case has an answer&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;orderTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Order not found"&lt;/span&gt;
    &lt;span class="c1"&gt;// From here, order can't be null, so no ?. or !! is needed&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Order total: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first version hides a decision. Nobody said what should happen when the order is missing. The second version answers that question with &lt;code&gt;?:&lt;/code&gt;, and after that line the compiler knows &lt;code&gt;order&lt;/code&gt; is not null.&lt;/p&gt;

&lt;p&gt;In code review, treat every &lt;code&gt;!!&lt;/code&gt; as a question: "Why can't this be null here?" If there is a good reason, show it in code. For example: &lt;code&gt;requireNotNull(order) { "Order must be loaded first" }&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where can a NullPointerException still happen in Kotlin?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://kotlinlang.org/docs/null-safety.html" rel="noopener noreferrer"&gt;Kotlin docs&lt;/a&gt; list a few causes. In everyday Android work, two matter most: &lt;code&gt;!!&lt;/code&gt; and Java code.&lt;/p&gt;

&lt;p&gt;Java does not say if a value can be null. So when Kotlin calls a Java method, the result has a &lt;a href="https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types" rel="noopener noreferrer"&gt;platform type&lt;/a&gt;. The IDE shows it as &lt;code&gt;String!&lt;/code&gt;, which means "&lt;code&gt;String&lt;/code&gt; or &lt;code&gt;String?&lt;/code&gt;, you decide".&lt;/p&gt;

&lt;p&gt;Say an old Java class has &lt;code&gt;public String getCouponCode(String orderId)&lt;/code&gt;, and it returns null when there is no coupon. In Kotlin:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;val code: String = cart.getCouponCode(orderId)&lt;/code&gt; compiles. At runtime it crashes on that line with "getCouponCode(...) must not be null".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;val code: String? = cart.getCouponCode(orderId)&lt;/code&gt; compiles, and now the compiler makes you handle null.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So treat values from Java as nullable unless you know better. "Knowing better" means the Java code has &lt;a href="https://kotlinlang.org/docs/java-interop.html#nullability-annotations" rel="noopener noreferrer"&gt;nullability annotations&lt;/a&gt; like &lt;code&gt;@Nullable&lt;/code&gt; and &lt;code&gt;@NonNull&lt;/code&gt;. Kotlin reads them and gives you a real &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;String?&lt;/code&gt; instead of &lt;code&gt;String!&lt;/code&gt;. If you own the Java code, &lt;a href="https://developer.android.com/kotlin/interop#nullability-annotations" rel="noopener noreferrer"&gt;Android's Kotlin interop guide&lt;/a&gt; says to annotate every public parameter, return value and field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which null tool should you use?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You write&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;Use it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;String?&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;This value can be null&lt;/td&gt;
&lt;td&gt;The value can really be missing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;if (x != null)&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Check, then use it as non-null&lt;/td&gt;
&lt;td&gt;You need several steps with the value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;x?.total&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Total, or null if &lt;code&gt;x&lt;/code&gt; is null&lt;/td&gt;
&lt;td&gt;Null is fine for the result too&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;x ?: default&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Use a default when null&lt;/td&gt;
&lt;td&gt;You have a good fallback, or want to return early&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;x?.let { }&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Run the block only when not null&lt;/td&gt;
&lt;td&gt;One action that needs the value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;x as? Type&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cast, or null if it fails&lt;/td&gt;
&lt;td&gt;You are not sure of the type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;code&gt;x!!&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Crash if null&lt;/td&gt;
&lt;td&gt;Almost never; prefer a check or &lt;code&gt;requireNotNull&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why does the smart cast not work on my &lt;code&gt;var&lt;/code&gt; property?&lt;/strong&gt;&lt;br&gt;
Because the property could change between the check and the use. The compiler tells you the smart cast is impossible. Copy it into a local &lt;code&gt;val&lt;/code&gt; first, then check that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is &lt;code&gt;!!&lt;/code&gt; always wrong?&lt;/strong&gt;&lt;br&gt;
No, but it should be rare. In tests it can be fine, because a crash there just fails the test. In app code, &lt;code&gt;requireNotNull&lt;/code&gt; or &lt;code&gt;checkNotNull&lt;/code&gt; with a message gives the same stop with a clearer reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the difference between &lt;code&gt;as&lt;/code&gt; and &lt;code&gt;as?&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;as&lt;/code&gt; throws a &lt;code&gt;ClassCastException&lt;/code&gt; if the value is not that type. &lt;code&gt;as?&lt;/code&gt; returns null instead, so you handle it with &lt;code&gt;?.&lt;/code&gt; or &lt;code&gt;?:&lt;/code&gt; like any other nullable value.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you remember?
&lt;/h2&gt;

&lt;p&gt;Kotlin does not remove null. It makes you say, in the type, where null is allowed. Then the compiler checks every place you use it.&lt;/p&gt;

&lt;p&gt;In everyday Kotlin code, watch two doors: &lt;code&gt;!!&lt;/code&gt; and Java. Keep both small and easy to see.&lt;/p&gt;

&lt;p&gt;If you want to test yourself, try the &lt;a href="https://imayankkumar.com/interview/kotlin-null-safety/" rel="noopener noreferrer"&gt;null safety interview questions&lt;/a&gt;. This post is the first topic in Stage 1 of my &lt;a href="https://imayankkumar.com/roadmap/" rel="noopener noreferrer"&gt;Android roadmap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Where do most &lt;code&gt;!!&lt;/code&gt; operators come from in your codebase: old Java code, framework callbacks, or habit?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/kotlin-null-safety/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Android Developer Roadmap 2027: What to Learn, What Interviews Ask, and Where AI Fits</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Sat, 31 Oct 2026 08:27:43 +0000</pubDate>
      <link>https://dev.to/imayankkumar/android-developer-roadmap-2027-what-to-learn-what-interviews-ask-and-where-ai-fits-2h8m</link>
      <guid>https://dev.to/imayankkumar/android-developer-roadmap-2027-what-to-learn-what-interviews-ask-and-where-ai-fits-2h8m</guid>
      <description>&lt;p&gt;If you are an Android developer right now, you are probably asking three questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;What should I learn next?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Does any of it still matter, now that AI writes code?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What will interviews actually ask me?&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last year did not make this easier. In May 2026, Google said &lt;a href="https://android-developers.googleblog.com/2026/05/android-ui-development-is-compose-first.html" rel="noopener noreferrer"&gt;Android UI is now Compose-first&lt;/a&gt; and moved the View components that Compose replaces into maintenance mode: critical bug fixes only, no new features, but no plan to remove them. The same day, &lt;a href="https://android-developers.googleblog.com/2026/05/android-cli-stable-1-0-agent-development.html" rel="noopener noreferrer"&gt;Android CLI 1.0&lt;/a&gt; went stable so AI agents can build and run Android apps. And Google was reported to be &lt;a href="https://careers.northeastern.edu/blog/2026/05/13/googles-ai-assisted-coding-interview-2026-guide/" rel="noopener noreferrer"&gt;piloting coding interviews where candidates use Gemini&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post answers all three questions. It is a complete roadmap for 2027: what to learn, in what order, how deep to go, what interviewers ask at each stage, and where AI fits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; Learn Kotlin and the Android platform first, then coroutines, Compose and architecture, then data, testing, production and system design. Use AI at every stage, but only as fast as you can check what it gives you. Interviews test the same things, plus coding problems and design.&lt;/p&gt;

&lt;p&gt;I built this from the official &lt;a href="https://developer.android.com/" rel="noopener noreferrer"&gt;Android Developers&lt;/a&gt; and &lt;a href="https://kotlinlang.org/docs/" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt; documentation, recent interview write-ups that developers posted publicly, and what I have seen in production Android work. It is my own roadmap, not one published by Google or JetBrains.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What changed going into 2027&lt;/li&gt;
&lt;li&gt;How to approach 2027&lt;/li&gt;
&lt;li&gt;The roadmap at a glance&lt;/li&gt;
&lt;li&gt;
The nine stages (each with must know, go deeper, where to start, interview questions and a self-check)&lt;/li&gt;
&lt;li&gt;Where AI fits&lt;/li&gt;
&lt;li&gt;What Android interviews look like now&lt;/li&gt;
&lt;li&gt;
What to learn later, and where to start
&lt;/li&gt;
&lt;li&gt;
One project and a sample 6-month plan
&lt;/li&gt;
&lt;li&gt;FAQ&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What changed for Android developers going into 2027?
&lt;/h2&gt;

&lt;p&gt;A roadmap is only useful if it matches the platform you will ship on. These changes from the last year affect what you should learn, newest first.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;When&lt;/th&gt;
&lt;th&gt;What changed&lt;/th&gt;
&lt;th&gt;What it means for your learning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Feb 1, 2027&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Play blocks app updates that &lt;a href="https://developer.android.com/guide/practices/page-sizes" rel="noopener noreferrer"&gt;don't support 16 KB memory pages&lt;/a&gt; (apps targeting Android 15+)&lt;/td&gt;
&lt;td&gt;Know how native libraries and your build affect this, even if you never write C++&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;2027&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://support.google.com/android-developer-console/answer/16561738" rel="noopener noreferrer"&gt;Developer verification&lt;/a&gt; expands to all apps on certified Android devices&lt;/td&gt;
&lt;td&gt;Even apps shipped outside Play will need a verified developer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Aug 31, 2026&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;New apps and updates must &lt;a href="https://developer.android.com/google/play/requirements/target-sdk" rel="noopener noreferrer"&gt;target Android 16 (API 36)&lt;/a&gt;
&lt;/td&gt;
&lt;td&gt;You must know API 36 behavior changes, not just the APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Jul 2026&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Google says &lt;a href="https://android-developers.googleblog.com/2026/07/five-years-of-jetpack-compose.html" rel="noopener noreferrer"&gt;more than 68% of the top 1,000 apps&lt;/a&gt; use Compose in production&lt;/td&gt;
&lt;td&gt;Compose is the default skill employers expect&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;May 19, 2026&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://android-developers.googleblog.com/2026/05/android-ui-development-is-compose-first.html" rel="noopener noreferrer"&gt;Compose-first&lt;/a&gt;: Views get critical bug fixes only, no new features&lt;/td&gt;
&lt;td&gt;Learn Compose first; learn Views to maintain old code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;May 19, 2026&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://android-developers.googleblog.com/2026/05/android-cli-stable-1-0-agent-development.html" rel="noopener noreferrer"&gt;Android CLI 1.0&lt;/a&gt; and Android skills for AI agents&lt;/td&gt;
&lt;td&gt;Agents can build and run Android apps; your job shifts toward checking their work&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of this replaces the basics. Most of it rewards people who already understand lifecycle, state and the build.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should you approach 2027?
&lt;/h2&gt;

&lt;p&gt;Before the topic lists, five rules. They matter more than any single library.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fundamentals before tools.&lt;/strong&gt; Libraries change every year. Lifecycle, state, threading and data flow have not changed much in a decade. Learn the things that will still be true in 2030.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One project, not ten tutorials.&lt;/strong&gt; Build one app and improve it at every stage. You learn more from fixing your own design than from finishing someone else's.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use AI, but check it.&lt;/strong&gt; Let AI write, explain and suggest. Then prove it right with a test, a profiler or the docs. If you can't check it, you are not ready to ship it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Go deep in one area.&lt;/strong&gt; Be solid across all nine stages, then pick one to become known for: performance, architecture, testing, AI features or Kotlin Multiplatform (KMP). Depth is what makes a senior engineer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Show your work.&lt;/strong&gt; A GitHub repo, a short write-up or a talk proves more than a certificate. It also gives you real stories for interviews.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The roadmap at a glance
&lt;/h2&gt;

&lt;p&gt;Each stage depends on the one before it. Compose makes little sense until you understand state. State makes little sense until you understand lifecycle and process death. Skip a stage and you can still ship features, but you end up debugging by guessing. AI makes that worse, because it gives you more code to guess about.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ve3d8gltjjwumrziszg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ve3d8gltjjwumrziszg.png" alt="Android Developer Roadmap 2027 diagram: nine stages in learning order. Foundations: 1 Kotlin and DSA basics, 2 Android platform, 3 Coroutines and Flow. Building apps: 4 Compose UI, 5 Architecture, 6 Data and offline. Shipping at quality: 7 Testing, 8 Production, 9 System design. Each stage has a done-when check. Two AI tracks run under all stages: use AI to work faster, and build AI features. A side box lists what to learn when needed: Kotlin and Compose Multiplatform, Views, Fragments and RxJava, specialized APIs, and Wear OS, TV, Auto and XR." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What it covers&lt;/th&gt;
&lt;th&gt;You are done when you can...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;1. Kotlin and DSA basics&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Language, collections, generics, OOP, DSA, Git, Gradle&lt;/td&gt;
&lt;td&gt;Build and test a small Kotlin project and explain your choices&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;2. Android platform&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Components, lifecycle, process death, insets, permissions, background limits&lt;/td&gt;
&lt;td&gt;Say what happens when the OS kills your app, and prove state comes back&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3. Coroutines and Flow&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Structured concurrency, cancellation, dispatchers, StateFlow&lt;/td&gt;
&lt;td&gt;Handle cancel, failure and two requests at once without bugs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;4. Compose UI&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;State, recomposition, side effects, Navigation 3, adaptive layouts&lt;/td&gt;
&lt;td&gt;Build a multi-screen app with loading, error, empty and data states&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;5. Architecture&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;UI and data layers, one-way data flow, ViewModel, dependency injection, modules&lt;/td&gt;
&lt;td&gt;Defend every layer in a feature you designed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;6. Data and offline&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;HTTP, Room, DataStore, caching, sync, database migrations&lt;/td&gt;
&lt;td&gt;Keep a feature useful when the network is slow or gone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;7. Testing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fakes, ViewModel and Flow tests, Compose UI tests, CI&lt;/td&gt;
&lt;td&gt;Catch a real regression automatically&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;8. Production&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Profiling, jank, ANRs, Baseline Profiles, R8, security, releases&lt;/td&gt;
&lt;td&gt;Reproduce a problem, fix it, and show before and after numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;9. System design&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mobile design, sync, failure handling, internals&lt;/td&gt;
&lt;td&gt;Design an app and defend it against two alternatives&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you already work as an Android developer, do not start at stage 1 by default. Use the last column as a test, and start at the first stage you cannot pass.&lt;/p&gt;

&lt;h2&gt;
  
  
  The nine stages
&lt;/h2&gt;

&lt;p&gt;Every stage has the same parts: why it matters, what you must know, what to learn when you want to go deeper, where to start, the interview questions it prepares you for, and one exercise to test yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 1: Kotlin and DSA basics
&lt;/h3&gt;

&lt;p&gt;Kotlin is the language of Android. Knowing the syntax is easy. Knowing what the language does for you, and what it does not, takes longer. This stage also covers DSA (data structures and algorithms), which most Android interviews still test in a coding round.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Types: &lt;code&gt;val&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt;, type inference, null safety (&lt;code&gt;?.&lt;/code&gt;, &lt;code&gt;?:&lt;/code&gt;, &lt;code&gt;!!&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;), smart casts&lt;/li&gt;
&lt;li&gt;Functions: default and named arguments, extension and infix functions, lambdas, higher-order functions, &lt;code&gt;inline&lt;/code&gt; and &lt;code&gt;reified&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Classes: data, sealed, enum, object, companion object, value classes, interfaces, visibility&lt;/li&gt;
&lt;li&gt;Generics: variance (&lt;code&gt;in&lt;/code&gt;, &lt;code&gt;out&lt;/code&gt;), star projections (&lt;code&gt;*&lt;/code&gt;) and why they exist&lt;/li&gt;
&lt;li&gt;Collections: read-only vs mutable, sequences, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;fold&lt;/code&gt;, &lt;code&gt;groupBy&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Equality: &lt;code&gt;==&lt;/code&gt; vs &lt;code&gt;===&lt;/code&gt;, &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Idioms: scope functions, &lt;code&gt;by lazy&lt;/code&gt;, class delegation, &lt;code&gt;lateinit&lt;/code&gt;, exceptions&lt;/li&gt;
&lt;li&gt;Design: OOP, SOLID, and patterns you will meet on Android (builder, factory, observer, strategy, adapter, singleton)&lt;/li&gt;
&lt;li&gt;DSA basics: Big-O, arrays, strings, hash maps, linked lists, trees, graphs, recursion, basic dynamic programming&lt;/li&gt;
&lt;li&gt;Tools: Git, Gradle basics with the Kotlin DSL and version catalogs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Kotlin and Java interop (&lt;code&gt;@JvmStatic&lt;/code&gt;, &lt;code&gt;@JvmOverloads&lt;/code&gt;, platform types)&lt;/li&gt;
&lt;li&gt;Reading the bytecode Kotlin generates&lt;/li&gt;
&lt;li&gt;DSL builders, contracts, context parameters&lt;/li&gt;
&lt;li&gt;Annotation processing with KSP&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://kotlinlang.org/docs/" rel="noopener noreferrer"&gt;Kotlin docs&lt;/a&gt;, then &lt;a href="https://kotlinlang.org/docs/whatsnew24.html" rel="noopener noreferrer"&gt;what's new in Kotlin 2.4&lt;/a&gt; to see where the language is going.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;lateinit&lt;/code&gt; vs &lt;code&gt;by lazy&lt;/code&gt;: when would you use each?&lt;/li&gt;
&lt;li&gt;What does an extension function compile to? Can it access private members?&lt;/li&gt;
&lt;li&gt;Why can a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt; be passed where a &lt;code&gt;List&amp;lt;Any&amp;gt;&lt;/code&gt; is expected, but a &lt;code&gt;MutableList&amp;lt;String&amp;gt;&lt;/code&gt; cannot?&lt;/li&gt;
&lt;li&gt;(Senior) What does &lt;code&gt;inline&lt;/code&gt; change at the call site, and why does &lt;code&gt;reified&lt;/code&gt; need it?&lt;/li&gt;
&lt;li&gt;(Senior) How does &lt;code&gt;by lazy&lt;/code&gt; handle threads by default, and when would you change that?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Write a sealed &lt;code&gt;UiState&lt;/code&gt; with four states and a &lt;code&gt;when&lt;/code&gt; that handles them. Add a fifth state. Notice what the compiler does, and explain why that is the point of sealed types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 2: The Android platform
&lt;/h3&gt;

&lt;p&gt;This is the stage people skip, and it is where many production bugs live. The platform also keeps changing under you. Apps that target Android 16 &lt;a href="https://developer.android.com/about/versions/16/behavior-changes-16" rel="noopener noreferrer"&gt;can no longer opt out of edge-to-edge&lt;/a&gt;, get predictive back by default, and have orientation and resize limits ignored on large screens. Apps that target Android 17 &lt;a href="https://developer.android.com/about/versions/17/behavior-changes-17" rel="noopener noreferrer"&gt;lose the large-screen opt-out&lt;/a&gt; and need a new permission for local network access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;App components: Activity, Service, BroadcastReceiver, ContentProvider&lt;/li&gt;
&lt;li&gt;Intents: explicit and implicit, intent filters, &lt;code&gt;PendingIntent&lt;/code&gt;, deep links and App Links&lt;/li&gt;
&lt;li&gt;Lifecycles: Activity, Fragment (for existing code) and process lifecycle, process priority&lt;/li&gt;
&lt;li&gt;Configuration changes and process death: &lt;code&gt;SavedStateHandle&lt;/code&gt;, &lt;code&gt;rememberSaveable&lt;/code&gt;, what to save and what to reload&lt;/li&gt;
&lt;li&gt;Tasks, back stack and launch modes&lt;/li&gt;
&lt;li&gt;Context types and when each is safe to hold&lt;/li&gt;
&lt;li&gt;Resources, qualifiers and the manifest&lt;/li&gt;
&lt;li&gt;Permissions: runtime and special permissions, the photo picker, scoped storage&lt;/li&gt;
&lt;li&gt;Notifications and channels; foreground services and their types&lt;/li&gt;
&lt;li&gt;Background limits: Doze, App Standby, what can run when&lt;/li&gt;
&lt;li&gt;Windows: edge-to-edge, insets (space taken by system bars, the keyboard and cutouts), predictive back, large screens and foldables&lt;/li&gt;
&lt;li&gt;The main thread, Looper and Handler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Binder and inter-process communication&lt;/li&gt;
&lt;li&gt;Zygote and how an app process starts&lt;/li&gt;
&lt;li&gt;App Startup library, multi-window, App Bundles and feature delivery&lt;/li&gt;
&lt;li&gt;Reading every Android version's "behavior changes" page as a habit&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://developer.android.com/guide/components/activities/activity-lifecycle" rel="noopener noreferrer"&gt;Activity lifecycle&lt;/a&gt; and &lt;a href="https://developer.android.com/topic/libraries/architecture/saving-states" rel="noopener noreferrer"&gt;saving UI state&lt;/a&gt; guides.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Walk me through the Activity lifecycle when the user rotates the screen, then presses Home.&lt;/li&gt;
&lt;li&gt;How does a ViewModel survive a configuration change? Does it survive process death?&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;singleTop&lt;/code&gt; vs &lt;code&gt;singleTask&lt;/code&gt;: when would you use each?&lt;/li&gt;
&lt;li&gt;Started vs bound service, and when must a service run in the foreground?&lt;/li&gt;
&lt;li&gt;(Senior) How would you find the process-death bugs your app has today?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Open your app, fill a form, press Home, then run &lt;code&gt;adb shell am kill &amp;lt;your.package&amp;gt;&lt;/code&gt;. Open it again from Recents. Did the form come back?&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 3: Coroutines and Flow
&lt;/h3&gt;

&lt;p&gt;Many Android bugs that "happen sometimes" are concurrency bugs. This is also where AI-written code most often looks correct while being wrong.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suspend functions; &lt;code&gt;launch&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt;, &lt;code&gt;runBlocking&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Scopes: &lt;code&gt;CoroutineScope&lt;/code&gt;, &lt;code&gt;viewModelScope&lt;/code&gt;, &lt;code&gt;lifecycleScope&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Structured concurrency: Job, parent and child, why a scope waits for its children&lt;/li&gt;
&lt;li&gt;Dispatchers and &lt;code&gt;withContext&lt;/code&gt;; never blocking the main thread&lt;/li&gt;
&lt;li&gt;Cancellation: it is cooperative; &lt;code&gt;isActive&lt;/code&gt;, &lt;code&gt;ensureActive&lt;/code&gt;, rethrowing &lt;code&gt;CancellationException&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Exceptions: propagation, &lt;code&gt;CoroutineExceptionHandler&lt;/code&gt;, &lt;code&gt;SupervisorJob&lt;/code&gt;, &lt;code&gt;supervisorScope&lt;/code&gt;, timeouts&lt;/li&gt;
&lt;li&gt;Flow: cold streams, operators (&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;combine&lt;/code&gt;, &lt;code&gt;flatMapLatest&lt;/code&gt;, &lt;code&gt;debounce&lt;/code&gt;), &lt;code&gt;catch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;StateFlow and SharedFlow, &lt;code&gt;stateIn&lt;/code&gt; and &lt;code&gt;shareIn&lt;/code&gt;, &lt;code&gt;WhileSubscribed&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Collecting safely: &lt;code&gt;repeatOnLifecycle&lt;/code&gt;, &lt;code&gt;collectAsStateWithLifecycle&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Bridging callbacks with &lt;code&gt;callbackFlow&lt;/code&gt;; protecting shared state with &lt;code&gt;Mutex&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;WorkManager for work that must finish even if the app closes&lt;/li&gt;
&lt;li&gt;Testing with &lt;code&gt;runTest&lt;/code&gt; and test dispatchers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Continuations and the state machine the compiler generates&lt;/li&gt;
&lt;li&gt;Channels and &lt;code&gt;select&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flowOn&lt;/code&gt;, &lt;code&gt;buffer&lt;/code&gt;, &lt;code&gt;conflate&lt;/code&gt; and backpressure (when a producer is faster than its collector)&lt;/li&gt;
&lt;li&gt;Threads, thread pools and Java memory model basics&lt;/li&gt;
&lt;li&gt;RxJava interop for older code&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://kotlinlang.org/docs/coroutines-guide.html" rel="noopener noreferrer"&gt;Kotlin coroutines guide&lt;/a&gt;, then Android's &lt;a href="https://developer.android.com/kotlin/coroutines/coroutines-best-practices" rel="noopener noreferrer"&gt;coroutines best practices&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;launch&lt;/code&gt; vs &lt;code&gt;async&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;StateFlow vs SharedFlow: when would you use each?&lt;/li&gt;
&lt;li&gt;What happens to the other children when one child coroutine fails? How does &lt;code&gt;supervisorScope&lt;/code&gt; change that?&lt;/li&gt;
&lt;li&gt;Where should you make an API call in a ViewModel, and on which dispatcher?&lt;/li&gt;
&lt;li&gt;(Senior) Why is catching &lt;code&gt;CancellationException&lt;/code&gt; dangerous? (See the snippet in the AI section.)&lt;/li&gt;
&lt;li&gt;(Senior) How does Flow handle a fast producer and a slow collector?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Start two requests with &lt;code&gt;async&lt;/code&gt; inside &lt;code&gt;coroutineScope&lt;/code&gt;, and make one fail. What happens to the other? Switch to &lt;code&gt;supervisorScope&lt;/code&gt; and explain the difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 4: Compose UI
&lt;/h3&gt;

&lt;p&gt;Google now says &lt;a href="https://android-developers.googleblog.com/2026/05/android-ui-development-is-compose-first.html" rel="noopener noreferrer"&gt;all Android UI should be built with Compose&lt;/a&gt;. Views still work and get critical fixes, but no new features. The goal of this stage is not to make screens. It is to predict when a screen recomposes and why.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Composable functions, Row, Column, Box, and why modifier order matters&lt;/li&gt;
&lt;li&gt;Material 3 and theming&lt;/li&gt;
&lt;li&gt;State: &lt;code&gt;remember&lt;/code&gt;, &lt;code&gt;mutableStateOf&lt;/code&gt;, &lt;code&gt;rememberSaveable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;State hoisting and one-way data flow (UDF): state flows down to the UI, events flow up&lt;/li&gt;
&lt;li&gt;Recomposition, skipping and stability&lt;/li&gt;
&lt;li&gt;Side effects: &lt;code&gt;LaunchedEffect&lt;/code&gt;, &lt;code&gt;DisposableEffect&lt;/code&gt;, &lt;code&gt;rememberCoroutineScope&lt;/code&gt;, &lt;code&gt;derivedStateOf&lt;/code&gt;, &lt;code&gt;snapshotFlow&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Lazy lists and why keys matter&lt;/li&gt;
&lt;li&gt;&lt;a href="https://android-developers.googleblog.com/2025/11/jetpack-navigation-3-is-stable.html" rel="noopener noreferrer"&gt;Navigation 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Forms, text input and image loading&lt;/li&gt;
&lt;li&gt;Previews and CompositionLocal&lt;/li&gt;
&lt;li&gt;Semantics and accessibility&lt;/li&gt;
&lt;li&gt;Window size classes and adaptive layouts for phones, foldables and tablets&lt;/li&gt;
&lt;li&gt;Basic animation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;The three phases: composition, layout and draw&lt;/li&gt;
&lt;li&gt;Custom layouts, &lt;code&gt;Modifier.Node&lt;/code&gt; and Canvas drawing&lt;/li&gt;
&lt;li&gt;The snapshot system and the slot table (where Compose keeps state between recompositions)&lt;/li&gt;
&lt;li&gt;Compose compiler stability reports&lt;/li&gt;
&lt;li&gt;View interop for existing apps, Glance for widgets&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; &lt;a href="https://developer.android.com/develop/ui/compose/mental-model" rel="noopener noreferrer"&gt;Thinking in Compose&lt;/a&gt;, then the &lt;a href="https://developer.android.com/develop/ui/compose/performance" rel="noopener noreferrer"&gt;Compose performance guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What triggers recomposition?&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;remember&lt;/code&gt; vs &lt;code&gt;rememberSaveable&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;When would you use &lt;code&gt;LaunchedEffect&lt;/code&gt;, and what happens when its key changes?&lt;/li&gt;
&lt;li&gt;What makes a class stable, and why does it matter?&lt;/li&gt;
&lt;li&gt;(Senior) A list janks while scrolling. How do you find out why?&lt;/li&gt;
&lt;li&gt;(Senior) How does Compose remember state between recompositions? (the slot table)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Turn on recomposition counts in Layout Inspector and scroll a list. Find one composable that recomposes more than it should, and fix it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 5: Architecture
&lt;/h3&gt;

&lt;p&gt;Google's &lt;a href="https://developer.android.com/topic/architecture/recommendations" rel="noopener noreferrer"&gt;architecture recommendations&lt;/a&gt; are a good baseline. They strongly recommend a data layer with repositories, one-way data flow (UDF) and dependency injection. They recommend a domain layer only when business logic is reused across ViewModels or a ViewModel gets too complex. Architecture should solve a problem you have. A small app does not need every layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Layers: UI, data, and an optional domain layer&lt;/li&gt;
&lt;li&gt;ViewModel: what it owns, what it must never hold (a Context or a View)&lt;/li&gt;
&lt;li&gt;UI state: one &lt;code&gt;uiState&lt;/code&gt; as a &lt;code&gt;StateFlow&lt;/code&gt;, modeled with data and sealed classes&lt;/li&gt;
&lt;li&gt;One-time events such as navigation or a snackbar&lt;/li&gt;
&lt;li&gt;Repositories, data sources and a single source of truth&lt;/li&gt;
&lt;li&gt;MVVM vs MVI-style state handling&lt;/li&gt;
&lt;li&gt;Dependency injection: constructor injection first, Hilt modules, scopes and &lt;code&gt;@HiltViewModel&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Mapping models between layers; modeling errors&lt;/li&gt;
&lt;li&gt;SOLID applied to Android code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/modularization" rel="noopener noreferrer"&gt;Modularization&lt;/a&gt;: feature and core modules, api and impl modules&lt;/li&gt;
&lt;li&gt;Gradle convention plugins&lt;/li&gt;
&lt;li&gt;Clean Architecture and its trade-offs&lt;/li&gt;
&lt;li&gt;Other DI options and when manual DI is enough&lt;/li&gt;
&lt;li&gt;Google's Now in Android sample app as a reference&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://developer.android.com/topic/architecture" rel="noopener noreferrer"&gt;guide to app architecture&lt;/a&gt; and the &lt;a href="https://developer.android.com/topic/architecture/recommendations" rel="noopener noreferrer"&gt;recommendations page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why expose &lt;code&gt;StateFlow&lt;/code&gt; and not &lt;code&gt;MutableStateFlow&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;MVVM vs MVI: what problem does each solve?&lt;/li&gt;
&lt;li&gt;How does Hilt know how to build a ViewModel?&lt;/li&gt;
&lt;li&gt;When does a domain layer earn its place?&lt;/li&gt;
&lt;li&gt;(Senior) How would you split a large app into modules, and how do you stop modules from depending on each other badly?&lt;/li&gt;
&lt;li&gt;(Senior) Design the architecture for an SDK that other apps will embed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Take a ViewModel that calls Retrofit directly. Move the call into a repository, write a fake repository, and test the ViewModel without the network.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 6: Data, networking and offline
&lt;/h3&gt;

&lt;p&gt;Real apps are mostly about data that is late, missing or out of date. The interesting problems start when the network fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP, REST, status codes and API contracts&lt;/li&gt;
&lt;li&gt;JSON with kotlinx.serialization or Moshi&lt;/li&gt;
&lt;li&gt;Retrofit; OkHttp interceptors, timeouts and caching&lt;/li&gt;
&lt;li&gt;Room: entities, DAOs, Flow queries, relations, transactions, and database migrations (changing the schema without losing user data)&lt;/li&gt;
&lt;li&gt;DataStore (and SharedPreferences in older code)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/architecture/data-layer/offline-first" rel="noopener noreferrer"&gt;Offline-first&lt;/a&gt;: the local database as the source of truth&lt;/li&gt;
&lt;li&gt;Paging 3&lt;/li&gt;
&lt;li&gt;WorkManager for sync&lt;/li&gt;
&lt;li&gt;Errors, retries, backoff and timeouts&lt;/li&gt;
&lt;li&gt;Auth tokens and safe token refresh&lt;/li&gt;
&lt;li&gt;Image loading and caching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;WebSockets and push with FCM&lt;/li&gt;
&lt;li&gt;GraphQL and the Ktor client&lt;/li&gt;
&lt;li&gt;SQL indexes and query performance&lt;/li&gt;
&lt;li&gt;Conflict resolution for data edited on two devices&lt;/li&gt;
&lt;li&gt;Encrypting data at rest&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://developer.android.com/topic/architecture/data-layer" rel="noopener noreferrer"&gt;data layer guide&lt;/a&gt; and the &lt;a href="https://developer.android.com/topic/architecture/data-layer/offline-first" rel="noopener noreferrer"&gt;offline-first guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What does your app show when it is offline?&lt;/li&gt;
&lt;li&gt;How do you write a Room migration safely?&lt;/li&gt;
&lt;li&gt;Five requests fail at once because the token expired. How do you refresh it only once?&lt;/li&gt;
&lt;li&gt;How would you build instant search without sending a request on every key press?&lt;/li&gt;
&lt;li&gt;(Senior) Design sync for an app that is used offline for days.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Make a list screen read only from Room and refresh from the network in the background. Turn on airplane mode. Does the screen still make sense?&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 7: Testing and code quality
&lt;/h3&gt;

&lt;p&gt;A feature that worked once on your phone is not done. Google's guidance is to &lt;a href="https://developer.android.com/topic/architecture/recommendations" rel="noopener noreferrer"&gt;prefer fakes to mocks&lt;/a&gt; and to test ViewModels, the data layer, navigation and performance. The skill is choosing what to test, not writing more tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local tests vs instrumented tests, and when each is worth it&lt;/li&gt;
&lt;li&gt;JUnit and clear assertions&lt;/li&gt;
&lt;li&gt;Fakes vs mocks&lt;/li&gt;
&lt;li&gt;ViewModel and repository tests&lt;/li&gt;
&lt;li&gt;Coroutine tests: &lt;code&gt;runTest&lt;/code&gt;, test dispatchers, replacing &lt;code&gt;Dispatchers.Main&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Flow tests&lt;/li&gt;
&lt;li&gt;Room tests with an in-memory database&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/develop/ui/compose/testing" rel="noopener noreferrer"&gt;Compose UI tests&lt;/a&gt; with semantics; Espresso for View code&lt;/li&gt;
&lt;li&gt;Lint and static analysis&lt;/li&gt;
&lt;li&gt;CI that runs tests on every pull request&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Screenshot tests&lt;/li&gt;
&lt;li&gt;Robolectric&lt;/li&gt;
&lt;li&gt;Coverage tools, and why coverage is not the goal&lt;/li&gt;
&lt;li&gt;Gradle Managed Devices and device farms&lt;/li&gt;
&lt;li&gt;Finding and fixing flaky tests&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; &lt;a href="https://developer.android.com/training/testing" rel="noopener noreferrer"&gt;Android testing fundamentals&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fake or mock: which do you prefer, and why?&lt;/li&gt;
&lt;li&gt;How do you test a ViewModel that uses &lt;code&gt;viewModelScope&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;What would you test first in a new feature?&lt;/li&gt;
&lt;li&gt;(Senior) Your UI tests are flaky in CI. What do you check first?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Break a feature on purpose, for example by swapping two lines in a mapper. Does any test fail? If not, write the one that would.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 8: Production engineering
&lt;/h3&gt;

&lt;p&gt;Shipping is a separate skill from building. In 2027 it also includes rules that did not exist a few years ago: &lt;a href="https://developer.android.com/guide/practices/page-sizes" rel="noopener noreferrer"&gt;16 KB page support&lt;/a&gt;, &lt;a href="https://developer.android.com/google/play/requirements/target-sdk" rel="noopener noreferrer"&gt;yearly target API deadlines&lt;/a&gt; and &lt;a href="https://support.google.com/android-developer-console/answer/16561738" rel="noopener noreferrer"&gt;developer verification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debugging: Logcat, breakpoints, Layout Inspector, Database and Network Inspector&lt;/li&gt;
&lt;li&gt;Memory: profiler, heap dumps, leaks, LeakCanary&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/performance/vitals/anr" rel="noopener noreferrer"&gt;ANRs&lt;/a&gt;: causes and how to read the traces&lt;/li&gt;
&lt;li&gt;Rendering: jank, frame timing, JankStats&lt;/li&gt;
&lt;li&gt;Startup: cold, warm and hot start&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/performance/baselineprofiles/overview" rel="noopener noreferrer"&gt;Baseline Profiles&lt;/a&gt; and Macrobenchmark&lt;/li&gt;
&lt;li&gt;R8, keep rules and app size&lt;/li&gt;
&lt;li&gt;Security: Keystore, network security config, Play Integrity, not shipping secrets in the APK&lt;/li&gt;
&lt;li&gt;Release: signing, App Bundles, Play testing tracks, staged rollouts, in-app updates&lt;/li&gt;
&lt;li&gt;Monitoring: crash reporting (for example Firebase Crashlytics), Remote Config and Android vitals&lt;/li&gt;
&lt;li&gt;Platform rules: target SDK upgrades, 16 KB pages, developer verification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Perfetto and custom trace sections; logging and network inspection tools such as Timber and Chucker&lt;/li&gt;
&lt;li&gt;Native crashes&lt;/li&gt;
&lt;li&gt;Gradle build speed and the configuration cache&lt;/li&gt;
&lt;li&gt;Feature flags and safe rollouts&lt;/li&gt;
&lt;li&gt;Privacy reviews and the Play Data safety form&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://developer.android.com/topic/performance/overview" rel="noopener noreferrer"&gt;performance overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is the area I care most about, because "the app feels slow" is not a bug report you can fix. You need numbers first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What causes an ANR, and how do you debug one from production?&lt;/li&gt;
&lt;li&gt;How do you find a memory leak?&lt;/li&gt;
&lt;li&gt;How do Baseline Profiles make startup faster?&lt;/li&gt;
&lt;li&gt;(Senior) A release raises the crash rate. Walk me through the next hour.&lt;/li&gt;
&lt;li&gt;(Senior) How would you cut cold start time in half, and how would you prove it?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Measure cold start with Macrobenchmark. Add a Baseline Profile. Measure again and write down both numbers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 9: System design and senior skills
&lt;/h3&gt;

&lt;p&gt;This is where you stop asking "how do I build this screen" and start asking "what breaks, and who owns it". System design is not boxes and arrows. It is requirements, data models, failure cases and trade-offs you can defend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clarifying requirements: users, scale, offline needs, what matters most&lt;/li&gt;
&lt;li&gt;API design between the app and the backend&lt;/li&gt;
&lt;li&gt;Data models and caching strategy&lt;/li&gt;
&lt;li&gt;Offline sync and conflicts&lt;/li&gt;
&lt;li&gt;Pagination and image loading pipelines&lt;/li&gt;
&lt;li&gt;Real-time updates: WebSocket, FCM, polling, and their trade-offs&lt;/li&gt;
&lt;li&gt;Background sync, retries and failure handling&lt;/li&gt;
&lt;li&gt;Auth and security&lt;/li&gt;
&lt;li&gt;Logging, metrics and monitoring&lt;/li&gt;
&lt;li&gt;Module boundaries and team ownership&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common design questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Chat app, news or social feed, photo gallery, offline notes or to-do, cart with filters, map or ride tracking, video player, file upload, LRU cache, an SDK for other apps.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Go deeper
  &lt;ul&gt;
&lt;li&gt;Android internals: ART and garbage collection, Looper and MessageQueue, the rendering pipeline (Choreographer, RenderThread, vsync)&lt;/li&gt;
&lt;li&gt;Leading migrations across a large codebase&lt;/li&gt;
&lt;li&gt;Writing design docs and mentoring&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to start:&lt;/strong&gt; the &lt;a href="https://developer.android.com/topic/architecture" rel="noopener noreferrer"&gt;guide to app architecture&lt;/a&gt; and the &lt;a href="https://developer.android.com/topic/modularization" rel="noopener noreferrer"&gt;modularization guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design a chat app that works offline.&lt;/li&gt;
&lt;li&gt;Design an image-heavy feed like Instagram's.&lt;/li&gt;
&lt;li&gt;Design an LRU cache.&lt;/li&gt;
&lt;li&gt;(Senior) Design an SDK that other teams will embed. What does its API look like, and how do you version it?&lt;/li&gt;
&lt;li&gt;(Senior) How do you roll out a risky change to millions of users safely?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Try this:&lt;/strong&gt; Design an offline-first chat app on paper. Then list three ways it fails, such as messages sent twice, out of order, or lost on process death, and how your design handles each.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does AI fit in an Android roadmap?
&lt;/h2&gt;

&lt;p&gt;AI is not stage 10. It runs alongside every stage, in two different ways: using AI to do your work, and building AI into the apps you ship.&lt;/p&gt;

&lt;h3&gt;
  
  
  Track A: Using AI as an Android engineer
&lt;/h3&gt;

&lt;p&gt;Google now ships tools for this. &lt;a href="https://android-developers.googleblog.com/2026/05/android-cli-stable-1-0-agent-development.html" rel="noopener noreferrer"&gt;Android CLI 1.0&lt;/a&gt; lets agents such as Gemini in Android Studio, Claude Code or Codex create, build and deploy Android apps. Android skills give agents Google's best practices for tasks like adaptive UI or testing setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Giving the tool the right context and clear limits&lt;/li&gt;
&lt;li&gt;Working in small steps: plan, small change, review, test&lt;/li&gt;
&lt;li&gt;Reviewing AI code for lifecycle, threading, state and security bugs&lt;/li&gt;
&lt;li&gt;Checking that AI-written tests can fail, not only that they pass&lt;/li&gt;
&lt;li&gt;What code and data you are allowed to share with which tool&lt;/li&gt;
&lt;li&gt;Knowing when to stop prompting and read the docs or the source&lt;/li&gt;
&lt;li&gt;The basics of how agents and MCP servers connect tools to models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the kind of code AI tools often write. It compiles, the happy path works, and the review looks clean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Almost right&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;loadProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onFailure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// also logs normal cancellations&lt;/span&gt;
            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Right&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;loadProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CancellationException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;                            &lt;span class="c1"&gt;// let cancellation finish&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;runCatching&lt;/code&gt; catches every &lt;code&gt;Throwable&lt;/code&gt;, including &lt;code&gt;CancellationException&lt;/code&gt;. When the user leaves the screen and the coroutine is cancelled, the first version treats that as a failure. It logs an error that never happened and runs code after the coroutine should have stopped. The &lt;a href="https://kotlinlang.org/docs/coroutines-cancellation.html" rel="noopener noreferrer"&gt;Kotlin docs&lt;/a&gt; say it directly: if you catch &lt;code&gt;CancellationException&lt;/code&gt;, rethrow it.&lt;/p&gt;

&lt;p&gt;You only spot this if you learned stage 3. That is why AI makes the fundamentals more valuable, not less.&lt;/p&gt;

&lt;h3&gt;
  
  
  Track B: Building AI features into apps
&lt;/h3&gt;

&lt;p&gt;Google's &lt;a href="https://developer.android.com/ai/overview" rel="noopener noreferrer"&gt;guide to choosing an AI solution&lt;/a&gt; starts with one rule: use the simplest model and tooling that does the job.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Good for&lt;/th&gt;
&lt;th&gt;Watch out for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;On-device (&lt;a href="https://developer.android.com/ai/gemini-nano" rel="noopener noreferrer"&gt;Gemini Nano&lt;/a&gt;, ML Kit GenAI APIs, Prompt API)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Privacy, offline use, no cost per request&lt;/td&gt;
&lt;td&gt;Only some devices support it; best for short text tasks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cloud (Firebase AI Logic, your own backend)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Larger models, image and video input, complex tasks&lt;/td&gt;
&lt;td&gt;Network, latency, cost, user data leaving the device&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Hybrid&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;On-device when the device supports it, cloud as fallback&lt;/td&gt;
&lt;td&gt;Two code paths to build and test&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Must know&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choosing on-device, cloud or hybrid for a feature&lt;/li&gt;
&lt;li&gt;Calling a model and streaming the answer into Compose state&lt;/li&gt;
&lt;li&gt;Structured output and parsing it safely&lt;/li&gt;
&lt;li&gt;Timeouts, retries, rate limits and errors&lt;/li&gt;
&lt;li&gt;Cost and latency budgets&lt;/li&gt;
&lt;li&gt;Keeping API keys off the device and user data safe&lt;/li&gt;
&lt;li&gt;Checking model output and handling wrong answers in the UI&lt;/li&gt;
&lt;li&gt;AI-friendly UX: showing progress, letting users correct the result&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://android-developers.googleblog.com/2026/07/build-intelligent-android-apps-appfunctions.html" rel="noopener noreferrer"&gt;AppFunctions&lt;/a&gt;, so on-device agents can use your app's features&lt;/li&gt;
&lt;li&gt;Classic ML Kit for vision and text tasks that don't need a large model&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Go deeper:&lt;/strong&gt; embeddings and retrieval, evaluating model output with test sets, building agents.&lt;/p&gt;

&lt;p&gt;Every item on that list is a stage 3, 6, 7 or 8 skill with a model in the middle. AI features do not replace the roadmap. They test it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI interview questions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Review this AI-written ViewModel and find the bugs.&lt;/li&gt;
&lt;li&gt;How do you check AI-generated code before you merge it?&lt;/li&gt;
&lt;li&gt;On-device or cloud for this feature, and why?&lt;/li&gt;
&lt;li&gt;How do you stop a model call from leaking user data or your API key?&lt;/li&gt;
&lt;li&gt;What is an MCP server, and how does it connect a tool to a model?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What do Android interviews look like now?
&lt;/h2&gt;

&lt;p&gt;Based on recent Android interview experiences that developers shared publicly in 2026, from SDE-1 to SDE-2, a few patterns stand out. They are personal stories, not a survey, but they point the same way:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Coding problems still show up.&lt;/strong&gt; Most loops had a DSA round (sliding window, two pointers, hash maps). Some asked for a small coroutine task instead. Android experience does not let you skip them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Android round goes deep, not wide.&lt;/strong&gt; ViewModel internals and keeping state across configuration changes came up again and again. So did coroutines. Interviewers ask "how does it work" and "what happens if", not "what is".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design starts early.&lt;/strong&gt; Even SDE-1 loops can have a design round. Topics included an image loading library, an analytics SDK, an infinite image grid and a shopping app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Some companies add a machine coding round.&lt;/strong&gt; You build a small working app live in 45 to 60 minutes, or as a take-home.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AI has entered the interview.&lt;/strong&gt; It comes up as a topic (which tools you use, what MCP is, how you use AI at work). In May 2026, Google was also reported to be &lt;a href="https://careers.northeastern.edu/blog/2026/05/13/googles-ai-assisted-coding-interview-2026-guide/" rel="noopener noreferrer"&gt;piloting a round&lt;/a&gt; where junior and mid-level candidates read, debug and improve existing code with Gemini.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How to prepare for each round&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Round&lt;/th&gt;
&lt;th&gt;What they test&lt;/th&gt;
&lt;th&gt;How to prepare&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Coding (DSA)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Problem solving under time pressure&lt;/td&gt;
&lt;td&gt;A few medium problems every week from month one. Explain your approach out loud before you code.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Android and Kotlin deep dive&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stages 1 to 5&lt;/td&gt;
&lt;td&gt;Use the interview questions in each stage. Answer out loud, then check yourself against the docs.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Machine coding&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Clean, working code fast&lt;/td&gt;
&lt;td&gt;Practice building a list, detail and search app in 60 minutes. Have a project template you know well.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Mobile system design&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stages 6 to 9&lt;/td&gt;
&lt;td&gt;Practice the common design questions: requirements first, then data, API, caching, offline, failure cases.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AI-assisted coding (where offered)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reading code, checking AI output&lt;/td&gt;
&lt;td&gt;Practice fixing bugs in code you didn't write, with AI, while explaining every change you accept.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Hiring manager and behavioral&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Ownership and judgment&lt;/td&gt;
&lt;td&gt;Prepare four true stories: a hard bug, a disagreement, a mistake, something you led.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What is asked depends on the company and the level. Do not count how many questions you memorized. Count how many answers you can explain, build and defend against "what if".&lt;/p&gt;

&lt;h2&gt;
  
  
  What can you learn later?
&lt;/h2&gt;

&lt;p&gt;A roadmap that makes everything look equally important does not help anyone. These are worth learning when your product, team or next role needs them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Kotlin Multiplatform and Compose Multiplatform.&lt;/strong&gt; JetBrains made &lt;a href="https://blog.jetbrains.com/kotlin/2025/05/compose-multiplatform-1-8-0-released" rel="noopener noreferrer"&gt;Compose Multiplatform for iOS stable&lt;/a&gt; in May 2025, and KMP is a main focus on the &lt;a href="https://kotlinlang.org/docs/roadmap.html" rel="noopener noreferrer"&gt;Kotlin roadmap&lt;/a&gt;. It is a strong skill, but it builds on stages 1 to 7. Learn it after them, or when your team shares code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;XML Views, Fragments and RxJava.&lt;/strong&gt; Many production apps still use them, and interviews still ask about Fragment lifecycles. Learn enough to maintain and migrate them. For new code, Compose, coroutines and Flow come first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specialized APIs.&lt;/strong&gt; Bluetooth, camera, media, location, health, Wear OS, TV, Auto and XR. Learn them deeply when a product needs them. Everyone should still know the privacy and permission basics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rule is simple: fundamentals first, then go deep where the product takes you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where should you start based on your experience?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;New to Android (0 to 1 year).&lt;/strong&gt; Start at stage 1 and go in order. Google's &lt;a href="https://developer.android.com/courses/android-basics-compose/course" rel="noopener noreferrer"&gt;Android Basics with Compose&lt;/a&gt; course covers the first stages well. Start coding problems early. Do not jump to AI features or KMP yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Working Android developer (1 to 4 years).&lt;/strong&gt; Test yourself with the "done when" column. Stages 2, 3 and 7 are the easiest to skip while still shipping features: process death, cancellation and tests that catch real bugs. Check those first, then add system design.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Senior (5+ years).&lt;/strong&gt; Stages 8 and 9 are your job now. Add both AI tracks, and make sure you can still answer the stage 2 and 3 interview questions without notes, because you are the one reviewing everyone's AI-written code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Should you build many small apps or one real project?
&lt;/h2&gt;

&lt;p&gt;One project, improved stage by stage, teaches more than ten tutorial apps. It also gives you something real to talk about in interviews.&lt;/p&gt;

&lt;p&gt;A good roadmap project has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Three to five Compose screens&lt;/li&gt;
&lt;li&gt;A remote API plus Room, with offline behavior&lt;/li&gt;
&lt;li&gt;ViewModels, repositories and one &lt;code&gt;uiState&lt;/code&gt; per screen&lt;/li&gt;
&lt;li&gt;Loading, error, empty and data states&lt;/li&gt;
&lt;li&gt;Unit tests, a few UI tests and CI&lt;/li&gt;
&lt;li&gt;One performance problem you measured and fixed&lt;/li&gt;
&lt;li&gt;A README with an architecture diagram and the decisions you made&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once it is stable, add one AI feature that solves a real user problem, such as search or summaries. That single feature will touch streaming, errors, cost, privacy and testing. Add it last, not first.&lt;/p&gt;

&lt;h2&gt;
  
  
  How long does this take? A sample 6-month plan
&lt;/h2&gt;

&lt;p&gt;A sample plan for about 12 to 15 focused hours a week, if you can already program. At 8 to 10 hours a week, expect closer to 9 months. Skip any stage whose check you already pass.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Month 1:&lt;/strong&gt; Kotlin and DSA basics, Git and Gradle&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Month 2:&lt;/strong&gt; Android platform, lifecycle and process death&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Month 3:&lt;/strong&gt; Coroutines, Flow and Compose UI&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Month 4:&lt;/strong&gt; Architecture, data and offline&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Month 5:&lt;/strong&gt; Testing, performance and releases&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Month 6:&lt;/strong&gt; System design, project polish and mock interviews&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Three things run through all six months: a few coding problems every week, both AI tracks, and your one project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I am following this roadmap in public
&lt;/h2&gt;

&lt;p&gt;I have built Android apps for more than six years. I still do not think a roadmap is something you finish. It is something you go back to.&lt;/p&gt;

&lt;p&gt;At work I built an alert that keeps reminding a user every few minutes until they open an urgent message. The easy version, a timer inside the app, stops the moment Android kills the process, and Doze would delay it anyway. What worked was handing the repeat loop to WorkManager and saving the active message ID to disk instead of keeping it in memory. Process death, Doze and background limits are stage 2 topics, and that feature is where they stopped being interview theory for me.&lt;/p&gt;

&lt;p&gt;So I am going through these stages again, at production depth: read the docs, build a small example, break it, and explain it from memory. AI helps at every step, but it does not replace the testing or the checking.&lt;/p&gt;

&lt;p&gt;The posts will be here on the blog, and the videos on &lt;a href="https://www.youtube.com/@byMayankKumar" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. I will keep this roadmap updated as Android changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Will AI replace Android developers?&lt;/strong&gt; I don't think the evidence points that way. AI tools write more of the code, but more developers distrust their accuracy than trust it (&lt;a href="https://survey.stackoverflow.co/2025/ai#2-accuracy-of-ai-tools" rel="noopener noreferrer"&gt;46% vs 33%&lt;/a&gt;). Someone still has to decide what to build, check that it works on real devices and own it in production. Some interviews are starting to move the same way: from writing code from scratch toward reading, checking and fixing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I need Kotlin Multiplatform to get an Android job?&lt;/strong&gt; Not for most native Android roles. It helps when a team shares code with iOS or the web. Learn it after you are solid in Kotlin, coroutines, architecture and testing, since KMP builds on all of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do Android developers really need DSA?&lt;/strong&gt; Usually, yes. A coding round is still common in Android interviews, even at SDE-2. DSA also helps in daily work: picking the right collection, keeping list updates fast, sizing a cache. Practice a little every week instead of doing it all just before your interview.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build fundamentals, then go deep
&lt;/h2&gt;

&lt;p&gt;There is no single stack that guarantees an Android career in 2027. But the order is clear: Kotlin, the platform, concurrency, Compose, architecture, data, testing, production, then system design. AI runs alongside all of it, as a tool and as a feature. Interviews test the same path, plus coding problems and design.&lt;/p&gt;

&lt;p&gt;The goal is not to learn every tool.&lt;/p&gt;

&lt;p&gt;It is to understand problems, build things that work, and keep up as Android changes.&lt;/p&gt;

&lt;p&gt;Which stage are you working on right now, and which one do you keep putting off?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/android-developer-roadmap-2027/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>career</category>
      <category>ai</category>
    </item>
    <item>
      <title>AI Can Write My Kotlin. Who's Going to Debug It at 2 AM?</title>
      <dc:creator>Mayank Kumar</dc:creator>
      <pubDate>Wed, 28 Oct 2026 20:38:45 +0000</pubDate>
      <link>https://dev.to/imayankkumar/ai-can-write-my-kotlin-whos-going-to-debug-it-at-2-am-1lm1</link>
      <guid>https://dev.to/imayankkumar/ai-can-write-my-kotlin-whos-going-to-debug-it-at-2-am-1lm1</guid>
      <description>&lt;p&gt;It's 2:07 AM, and Crashlytics has opinions. The crash is in a ViewModel that &lt;code&gt;git blame&lt;/code&gt; says you wrote. You didn't. An agent did, and its pull request said "all tests pass." They do. The agent updated the expected values to match its new behaviour.&lt;/p&gt;

&lt;p&gt;That's a composite, not a confession. But it points at a real shift in Android work. This post covers what AI tools can do on Android today, why "the tests pass" is not the same as "it works" on a phone, and what to get better at.&lt;/p&gt;

&lt;p&gt;Short answer: AI can now write and fix Android code, but proving it works on a real device is still your job.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can AI tools do on Android today?
&lt;/h2&gt;

&lt;p&gt;Quite a lot. Android Studio's &lt;a href="https://android-developers.googleblog.com/2026/05/whats-new-android-developer-tools.html" rel="noopener noreferrer"&gt;May 2026 release&lt;/a&gt; includes an agent that plans multi-step work and fixes its own build errors, "Fix with AI" for crashes in App Quality Insights, and an option to hand LeakCanary traces to an agent. Google also runs &lt;a href="https://android-developers.googleblog.com/2026/03/elevating-ai-assisted-androi.html" rel="noopener noreferrer"&gt;Android Bench&lt;/a&gt;, which gives models real issues from public Android repos and checks each fix with tests. At its March 2026 launch, models solved between 16% and 72% of tasks.&lt;/p&gt;

&lt;p&gt;So writing Android code is no longer the slow part. Checking it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is "almost right" the expensive part?
&lt;/h2&gt;

&lt;p&gt;Because it compiles, looks fine in review, and fails later. In Stack Overflow's &lt;a href="https://survey.stackoverflow.co/2025/ai" rel="noopener noreferrer"&gt;2025 survey&lt;/a&gt;, 66% of developers named "AI solutions that are almost right, but not quite" as their top frustration. Google's &lt;a href="https://cloud.google.com/blog/products/ai-machine-learning/announcing-the-2025-dora-report" rel="noopener noreferrer"&gt;DORA 2025 report&lt;/a&gt; found that AI adoption goes with higher delivery throughput, but it's still linked to less stable delivery.&lt;/p&gt;

&lt;p&gt;When changes get cheap to produce, teams produce more of them. The limit becomes how much change you can verify.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this harder on Android?
&lt;/h2&gt;

&lt;p&gt;Because an agent works on the repository, but Android correctness depends on the device at runtime.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx9f9b2kg0xg25kg3z65g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx9f9b2kg0xg25kg3z65g.png" alt="Diagram: on the left, what the agent sees, the repository with a green build and passing tests. On the right, where Android bugs live: process death, OEM battery managers, R8 release builds, recomposition, and low-memory devices." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Process death, OEM battery managers, R8 in release builds, unexpected recomposition, and main-thread work on low-memory devices rarely show up in a diff. Most don't show up on a flagship emulator either.&lt;/p&gt;

&lt;p&gt;Here's what "almost right" looks like. Both versions compile and pass a simple unit test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Loses the draft when Android kills the app in the background&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ComposeViewModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;draft&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;mutableStateOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Restores the draft after process death&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ComposeViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;draft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"draft"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onDraftChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"draft"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first version keeps the draft only in memory. The second saves it in &lt;code&gt;SavedStateHandle&lt;/code&gt;, which Android restores when the user comes back.&lt;/p&gt;

&lt;p&gt;Android Bench counts a task as solved when the tests pass. That's fair for a benchmark and risky for a release. Tests only check what someone thought to check, and an agent working toward green will take the shortest path there. Sometimes that path goes through the test file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Doesn't AI debug too?
&lt;/h2&gt;

&lt;p&gt;It does, and it's improving. At 2 AM, an agent can give you three possible explanations for a crash in seconds. The hard part is picking the true one: what the evidence supports, and whether the fix treats the cause or just hides it behind a &lt;code&gt;?.let&lt;/code&gt;. That takes a mental model of the system.&lt;/p&gt;

&lt;p&gt;Two things are less certain. Simple apps may end up mostly built by agents, so deep Android skill will matter most where failure is expensive. And if junior engineers stop writing the boring code, it's not clear how they'll build that mental model. I don't have an answer to that yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's coming next?
&lt;/h2&gt;

&lt;p&gt;Your app may get a second kind of user. &lt;a href="https://developer.android.com/ai/appfunctions" rel="noopener noreferrer"&gt;AppFunctions&lt;/a&gt;, an experimental API on Android 16 and higher, lets an app expose functions that agents can call. The function's KDoc becomes the description the agent reads. That makes KDoc an interface contract, and it makes every argument LLM-generated input that you validate like any untrusted input.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you do now?
&lt;/h2&gt;

&lt;p&gt;Make checking cheaper than generating.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Habit&lt;/th&gt;
&lt;th&gt;What it catches&lt;/th&gt;
&lt;th&gt;How to start&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Process-death test&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;State lost when the user returns&lt;/td&gt;
&lt;td&gt;Background the app, run &lt;code&gt;adb shell am kill &amp;lt;package&amp;gt;&lt;/code&gt;, reopen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Release-build smoke test&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;R8 removing code you still need&lt;/td&gt;
&lt;td&gt;Run your critical flow on the minified build&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;One Macrobenchmark&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Performance regressions&lt;/td&gt;
&lt;td&gt;Measure startup or scrolling on your key screen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Small agent diffs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Changes nobody can review properly&lt;/td&gt;
&lt;td&gt;Ask for a plan first; read any edited tests first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Read traces&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fixes for the wrong cause&lt;/td&gt;
&lt;td&gt;Perfetto for jank, a heap dump for leaks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And write some code by hand on purpose, in the parts you want to understand deeply. That's how mental models get built.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can an AI agent test process death?&lt;/strong&gt; Only if your workflow asks it to. Tests check what someone thought to check, and that's true for agents too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should I stop using AI for Android code?&lt;/strong&gt; No. Let it write the code, and spend the time you save checking on real devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does Android Bench measure real-world quality?&lt;/strong&gt; It checks whether a model's fix passes tests from real repos. That's useful, but passing tests isn't the same as working on a phone.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, who debugs at 2 AM?
&lt;/h2&gt;

&lt;p&gt;You and an agent, together. The agent will produce explanations faster. Your job is knowing which one is true and owning the release.&lt;/p&gt;

&lt;p&gt;What's the most convincing "almost right" code an AI tool has given you on Android?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post first appeared on&lt;/em&gt; &lt;a href="https://imayankkumar.com/blog/ai-can-write-my-kotlin/" rel="noopener noreferrer"&gt;&lt;em&gt;imayankkumar.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, where you'll find new posts first.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>ai</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
