<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by MAHMOOD AHMAD on Medium]]></title>
        <description><![CDATA[Stories by MAHMOOD AHMAD on Medium]]></description>
        <link>https://medium.com/@gaudentas199?source=rss-183bcb1527cb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vB7R3QEh0wdGdOFE</url>
            <title>Stories by MAHMOOD AHMAD on Medium</title>
            <link>https://medium.com/@gaudentas199?source=rss-183bcb1527cb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:57:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gaudentas199/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kotlin Platform Types will break your apps.]]></title>
            <link>https://medium.com/@gaudentas199/kotlin-platform-types-will-break-your-apps-3193d4d31742?source=rss-183bcb1527cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/3193d4d31742</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[MAHMOOD AHMAD]]></dc:creator>
            <pubDate>Sun, 17 Nov 2024 08:05:37 GMT</pubDate>
            <atom:updated>2024-11-17T10:11:01.497Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dwcJk49D0LqJeWzcdFNICw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@osmanrana?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Osman Rana</a> on <a href="https://unsplash.com/photos/timelapse-photography-of-tunnel-8TLQMMJp_Y0?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><p>Have you ever stumbled upon Type! in Kotlin (while using the values of Java function) and thought, <em>What on Earth is this exclamation mark doing here?</em> Well, welcome to the fascinating world of <strong>Kotlin Platform Types</strong> — a key feature that silently mediates between Java and Kotlin.</p><p>And guess what? They might silently break your app. How? Let’s dive in.</p><p>But what is it? Come to the darn point.</p><blockquote>A platform type is a type originating from a language like Java, where its nullability is not explicitly defined or annotated, leaving Kotlin unable to determine whether it can be null or not.</blockquote><p>Platform types come into play when Kotlin interacts with languages like Java, which don’t explicitly define nullability.</p><ul><li>In Kotlin, nullability is part of the type system (likeString vs. String?).</li><li>In Java, nullability is… let’s just say <em>optional</em>. Without annotations, Kotlin doesn’t know if a value from Java is nullable or not, so it creates a <strong>Platform Type</strong>: String!.</li></ul><p>Platform types are Kotlin’s way of saying:</p><blockquote><em>“This type </em>might<em> be nullable, or it might not. You figure it out.”</em></blockquote><p>Consider</p><pre>public class JavaClass {<br>    public String getValue() {<br>        return null;<br>    }<br>} <br>// Legacy Java code that Kotlin will use</pre><p>And below sample how it will be used.</p><pre>fun platformType() {<br>    val value = JavaClass().value<br>    println(value.length)<br>}<br><br>fun statedType() {<br>    val value: String = JavaClass().value<br>    println(value.length)<br>}<br><br>fun main() {<br>    platformType()<br>    statedType()<br>} // Sample consumer</pre><p>But what’s the big deal you may say? Get to the point.<br>Look at the below screenshot carefully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*PSWKOu25RYSdIzVY0M0iGw.png" /></figure><p>Noticed something? Well you can see on line 4 that there is a inlay hint provided by the IDE that mentions the type of variable as `<strong>String!`. This is the platform type.</strong> Our Kotlin function isn’t able to infer its nullability at compile time.</p><p>Look at the above code snippet again carefully and try to identify where it will break your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*SLLJwudRJqxLSpriCbk1PA.gif" /><figcaption>You’re almost there.</figcaption></figure><p>For the function <strong>platformType(), </strong>the code will throw exception on line 5 because while accessing it will throw the infamous NPE — Null Pointer Exception.</p><p>For the function <strong>statedType(), </strong>the NPE will be thrown on line 9 because we’ve explicitly specified the return type to be non-null but its null.</p><p>Well we can avoid it in 2 steps.</p><ol><li>Annotate all Java methods with @NotNull or @Nullable and then handle the them on Kotlin side.</li><li>Without annotating java functions, let the Kotlin code handle that explicitly (like String? at every place of Java code invocation that gives a value). ← I won’t prefer this approach as it’s additional handling on consumer and breaking type inference feature of Kotlin.</li></ol><p>I would just show the first approach.</p><p>When we annotate the function with @Nullable.</p><pre>public class JavaClass {<br>    @Nullable<br>    public String getValue() {<br>        return null;<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*6nKyMV6KjS9sIMHpsth3Cg.png" /><figcaption>Notice how we’re able to identify the error at compile time itself.</figcaption></figure><p>And how we handle it varies across usecases. I’ll do it the following way</p><pre>fun platformType() {<br>    val value = JavaClass().value ?: &quot;&quot;<br>    println(value.length)<br>}<br><br>fun statedType() {<br>    // Doesn&#39;t even print anything<br>    val value = JavaClass().value ?: return<br>    println(value.length)<br>}<br><br>fun main() {<br>    platformType()<br>    statedType()<br>}</pre><p>Key Takeaways</p><ol><li>Platform Types = Null Ambiguity</li><li>They arise from Java code with no nullability annotations. Kotlin points the nullability decision to the developer.</li><li>Don’t Rely on Platform Types Blindly</li><li>Treat them cautiously. Assume the worst: a String! might be null.</li><li>Annotate Java Code Wherever Possible. Use @Nullable or @NotNull to declare intent and eliminate ambiguity.</li></ol><p><strong>Conclusion : </strong>Null safety is one of Kotlin’s superpowers, but Platform Types can weaken it when dealing with Java interop. By understanding how they work and using proper nullability annotations or defensive handling, you can avoid runtime NPE 💥💥💥 and ensure a safer codebase.</p><p>So, the next time you spot a String!, pause and think. Null safety isn’t just a feature—it’s a mindset.<br><strong>Learn it. Embrace it. And avoid the dreaded NPE.</strong></p><p>Got to know something new? Hit those 👏 claps and share the knowledge! 😊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*41WM1nYS9a5JlTyDGhzjTw.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3193d4d31742" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>