Chapter 3: Variables, Data Types & Null Safety (Kotlin’s Killer Feature)

Kotlin’s null safety is often called its killer feature — it practically eliminates the infamous NullPointerException (NPE) that has haunted Java developers for decades.

We’re going to go super slowly, like we’re sitting together in a Bandra café with cutting chai ☕, and I’ll explain every single concept with lots of real-life analogies, copy-paste examples, step-by-step breakdowns, tables, common mistakes, and fun facts so everything sticks perfectly.

Let’s begin!

1. val (Immutable) vs var (Mutable) – The Foundation

In Kotlin, you declare variables in two ways:

Keyword Meaning Can be reassigned? Real-life analogy
val Value – read-only No – once set, cannot change Like a printed book – you can read it, but you can’t change the text
var Variable – mutable Yes – can be changed later Like a notebook – you can erase and write new values

Important rule: Always prefer val unless you really need to change the value later. This makes your code safer, more predictable, and easier to reason about.

Example – val vs var

Kotlin

Fun fact: In real Kotlin code (especially Android & backend), ~80–90% of variables are val — that’s how powerful and safe it is!

2. Type Inference – Kotlin’s Smartness

Kotlin infers (guesses) the type automatically — you don’t have to write it every time.

Kotlin

When to write type explicitly?

  • When the compiler cannot infer (rare)
  • When you want to make code more readable for others
  • When working with ambiguous literals (e.g., 1 could be Int or Long)
Kotlin

3. All Basic Data Types in Kotlin

Kotlin has almost the same basic types as Java, but they are all objects under the hood (no primitives like Java’s int vs Integer).

Type Size / Range Example Literal Default Value
Int 32-bit integer 42, 1_000_000 (underscores OK) 0
Long 64-bit integer 42L, 1_000_000_000L 0L
Double 64-bit floating point 3.14, 9.8e2 0.0
Float 32-bit floating point 3.14f, 9.8e2f 0.0f
Boolean true / false true, false false
Char Single 16-bit Unicode character ‘A’, ‘न’, ‘\u20B9’ (₹)
String Sequence of characters “Hello”, “नमस्ते”

Special Kotlin features for literals:

Kotlin

String templates (super powerful):

Kotlin

4. Nullable Types – Kotlin’s Killer Feature (Null Safety)

In Java → String name = null; → name.length() → NullPointerException crash!

In Kotlin:

  • By default → non-nullable (cannot hold null)
  • Add ? to make it nullable
Kotlin

Safe ways to handle nullable types

Operator / Technique What it does Example
Safe call ?. Call only if not null, else return null nickname?.length → null if nickname is null
Elvis operator ?: Return left if not null, else right (default) nickname ?: “Guest”
Not-null assertion !! I promise it’s not null – crash if it is nickname!!.length (dangerous!)
Smart cast Compiler automatically knows after check if (nickname != null) { nickname.length }
let { } Safe scope when not null nickname?.let { println(it.length) }

Example – All in Action

Kotlin

5. Platform Types & Java Interop Nullability

When calling Java code from Kotlin, Kotlin doesn’t know if a Java type is nullable or not → it becomes a platform type (marked with !).

Example:

Kotlin

How to handle:

Kotlin

Best practice: Use nullability annotations in Java code (@Nullable, @NotNull from JetBrains or AndroidX) → Kotlin understands them perfectly.

6. Quick Recap Table (Your Cheat Sheet)

Concept Kotlin Way (Best Practice) Java Equivalent (for comparison)
Immutable variable val name = “Webliance” final String name = “Webliance”
Mutable variable var age = 25 int age = 25
Type inference val city = “Mumbai” (no type needed) Must write String city = “Mumbai”
Nullable type String? String (can be null)
Safe call name?.length if (name != null) name.length()
Elvis operator name ?: “Guest” name != null ? name : “Guest”
Not-null assertion name!! (String) name (risky cast)

7. Homework for You (Let’s Make It Fun!)

  1. Basic Declare 5 val variables: your name, age, city, salary (Double), isStudent (Boolean). Print them using string templates.
  2. Medium Create a nullable String? email variable. Print its length using safe call and Elvis operator.
  3. Advanced Write a function fun greet(name: String?) that prints “नमस्ते $name!” if name is not null, else “नमस्ते Guest!”
  4. Fun Create a var score = 85. Increase it by 10, then print “Your new score is $score!” using string template.
  5. Challenge Fix this buggy code:
    Kotlin

You’ve just unlocked Kotlin’s superpowernull safety! This single feature saves millions of hours of debugging worldwide.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *