Syntax

Basic syntax

Category Java Kotlin Notes

Program Entry Point

public static void main(String[] args) { …​ }

fun main(args: Array<String>) { …​ }

Visibility Modifiers

private, package private (default), protected, and public

private, protected, internal, and public` (default)

Types

void
int, Integer

Unit
Int

Everything is an object in Kotlin.

Object Creation

MyClass obj = new MyClass();

val obj = MyClass()

No new keyword in Kotlin.

Variables

final int y = 6; (immutable)
int x = 5; (mutable)

val y = 6 (immutable)
var x = 5 (mutable)

val is like final in Java.

Switch / When

switch (x) { case 1: …​ break; …​ }

when(x) { 1 → …​; else → …​ }

Kotlin when is expression, supports ranges, types, multiple values, no fallthrough.

Enums

enum Color { RED, GREEN, BLUE }

enum class Color { RED, GREEN, BLUE }

Null Safety (Type System)

All types nullable by default (String s = null;)

Non-nullable by default (var s: String = "hello") and nullable (var s: String? = null)

Avoids NullPointerExceptions at compile time.

Null Safety (Operators)

Manual null checks

val len = s?.length ?: 0

Safe calls (?.) + Elvis (?:) operators reduce NPEs.

Default Parameters

Not supported; need overloaded constructors/methods

Supported: fun greet(name: String = "World")

Reduces boilerplate, improves readability.

Paradigms

Object-oriented, some functional (since Java 8 with lambdas, streams)

Multiparadigm: OOP + FP + DSL-friendly

Kotlin integrates FP features natively.

Classes / OOP

class MyClass {}

class MyClass;
open class MyClass for inheritance

Classes final by default in Kotlin;
no open → safer design.

Inheritance

class Child extends Parent {}

class Child : Parent()

: instead of extends, implements;
open required on parent class.

Interfaces

interface I { void foo(); }

interface I { fun foo() }

Constructors

Single constructor or multiple overloads

Primary constructor + secondary constructors

Supports default arguments;
Java interop may require secondary constructors.

Properties / Getters-Setters

Fields + manual getters/setters

var/val prop: Type auto-generates getter/setter

Cleaner, reduces boilerplate.

Data Classes / Value Objects

Java 14+: record Person(String name) {} auto-generates equals, hashCode, toString

data class Person(val name: String) auto-generates equals, hashCode, toString, copy, componentN

Functional Programming

Lambdas (Java 8+), Streams API

Lambdas, higher-order functions, extension functions, sequences

Kotlin integrates FP throughout;
extension functions are a big advantage.

Collections

List<String> list = new ArrayList<>();

val list = listOf("a", "b");
mutable mutableListOf

Immutable by default;
concise literals.

Type Inference

Limited; must declare type explicitly

Strong type inference

val x = 42 → compiler infers Int.

Extension Functions

fun String.hello() = "Hi $this"

Adds methods to existing classes without inheritance.

Exception Handling

Checked exceptions (throws)

No checked exceptions;
only runtime exceptions

Simplifies error handling;
avoids try/catch boilerplate.

String Templates

Concatenation: "Hello " + name

"Hello $name"

Cleaner string interpolation.

Loops / Iteration

for (int i = 0; i < 10; i++) {}

for (i in 0..9); for (c in string); for ((k,v) in map)

Idiomatic Kotlin uses ranges and destructuring.

Pattern Matching / Type Checks

instanceof + cast

is + smart cast + when

Kotlin smart casts remove explicit casting.

Companion Object / Static

static fields/methods

companion object

Provides static-like behavior with full OO support.

Delegation

Manual forwarding

class MyClass(b: Base) : Base by b

Cleaner way to implement interfaces by delegation.

Coroutines / Concurrency

Threads, Executors, CompletableFuture

Coroutines (suspend fun), channels, flows

Lightweight concurrency, easier async programming.

Pattern Matching / Switch Enhancements

Java 17+: switch expressions & pattern matching

when (more general)

Kotlin had it first;
more expressive.