Kotlin vs Java

Same Program in Both Languages

A Java program needs a class and a public static void main method:

public class Main {
    public static void main(String[] args) {
        String name = "Asha";
        int age = 22;
        System.out.println("Hello " + name + ", you are " + age);
    }
}

Example

The Kotlin version is shorter: no class, no semicolons, types are inferred and values go straight into the string.

fun main() {
    val name = "Asha"
    val age = 22
    println("Hello $name, you are $age")
}

Output

Hello Asha, you are 22

Key Differences

FeatureJavaKotlin
Semicolonsrequiredoptional (normally omitted)
Null safetyany reference can be nullString can never be null, String? can
Variablesint x = 5; / finalvar x = 5 / val x = 5
Type inferenceonly var for locals (Java 10+)everywhere
String formatting"Hi " + name"Hi $name" (string templates)
switchswitchwhen (more powerful, is an expression)
Checked exceptionsyesno
Primitive typesint, double + wrappersonly Int, Double; compiler picks the primitive
Data holdersclass + getters/setters/equals (or records)data class in one line

Tip: You do not have to choose one forever. Kotlin calls Java code and Java calls Kotlin code in the same project, so teams migrate old Android apps one file at a time.