Introduction

1. What is Kotlin?

Kotlin is a modern programming language mainly used for Android app development.

It is simple, safe, and works with Java.

Simple Kotlin Program

fun main() {
    println("Hello Kotlin")
}

Output

Hello Kotlin

Explanation

CodeMeaning
funUsed to create a function
main()Starting point of a Kotlin program
println()Prints output on the screen


2. Kotlin Features

Kotlin has many useful features.

FeatureSimple Meaning
Concise codeLess code compared to Java
Null safetyHelps avoid null pointer crash
Type inferenceKotlin detects data type automatically
Java interoperabilityKotlin can use Java code
Data classesEasy way to create model classes
Extension functionsAdd new functions to existing classes
CoroutinesUsed for background tasks
Smart castKotlin automatically converts checked types


Example: Kotlin is Concise

Java Code

String name = "Mahesh";
System.out.println(name);

Kotlin Code

val name = "Mahesh"
println(name)

Simple meaning:

Kotlin does the same work with fewer lines and cleaner syntax.


3. Kotlin vs Java

PointJavaKotlin
Code lengthMore codeLess code
Null safetyManual handlingBuilt-in null safety
SemicolonUsually usedNot required
Variablesfinal, normal variableval, var
Data classNeed constructor, getter, setterOne line
Android supportSupportedPreferred
Functional featuresLimitedStrong support
CoroutinesNot built-inEasy support


Example: Model Class

Java

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Kotlin

data class User(
    val name: String,
    val age: Int
)

Simple meaning:

Kotlin removes boilerplate code like constructor, getter, and setter.