Java & Kotlin
Java & Kotlin - Comparative Overview and Practical Examples
Understand JVM fundamentals and how Java and Kotlin run on the
JVM.
Master core Java features used in real-world apps (OOP, generics,
concurrency).
Learn Kotlin's modern language features (null-safety, data classes,
coroutines).
Practice interoperability (call Java from Kotlin and Kotlin from Java).
Be able to choose which language/features to use and why.
JVM
JVM EXECUTES BYTECODE JRE PROVIDES RUNTIME LANGUAGES COMPILE TO JVM CLASSPATH / MODULARITY (JAVA 9
PRODUCED BY COMPILERS (JAVAC, (CLASSLOADER, JIT, GC). BYTECODE - JAVA AND KOTLIN MODULE SYSTEM) AND JARS.
KOTLINC). INTEROPERATE AT BYTECODE LEVEL.
Java: High-level Overview
Statically typed, object-oriented (functional features since Java 8).
Mature ecosystem and large enterprise adoption.
Backward compatibility is a strong guarantee.
Build tools: Maven, Gradle. Standard library: java.* and javax.*.
Java: Basic Example (Class +
main)
Java: Collections & Streams
(Java 8+)
Java: Generics & Variance
Generics & variance:
- Declaration: class Box<T> { ... }
- Use-site variance via wildcards:
- List<? extends Number> (producer - covariant)
- List<? super Integer> (consumer - contravariant)
- Type erasure: generic types are erased at runtime.
Java: Concurrency Basics
Concurrency basics:
- Threads: new Thread(() -> {...}).start()
- Higher-level: ExecutorService, CompletableFuture
- Synchronization: synchronized, volatile, [Link] utilities.
Slide 8 - Java: Exceptions &
Checked Exceptions
Exceptions & checked exceptions:
- Checked exceptions (must be declared/handled): e.g., IOException.
- Unchecked exceptions: RuntimeException subclasses.
- Note: Kotlin does not have checked exceptions.
Java: Build & Tooling
Build: Maven ([Link]), Gradle ([Link] or Groovy).
IDEs: IntelliJ IDEA, Eclipse.
Testing: JUnit, Mockito.
Packaging: jar, war, spring-boot fat jars.
Kotlin: High-level Overview
- Modern statically typed language targeting JVM, JS, Native.
- Concise syntax, null-safety by design, coroutines for concurrency.
- First-class support in Android; full interop with Java.
- Compiles to JVM bytecode or Kotlin/JS or Kotlin/Native.
Kotlin: Basic Example
(Equivalent to Java Person)
Kotlin basic example:
- val = immutable, var = mutable. String templates with ${}.
Kotlin: Data Classes &
Boilerplate Reduction
Data class example:
- equals, hashCode, toString, copy, componentN functions are generated.
Kotlin: Null-Safety & Type
System
- Nullable types:
- Elvis operator: val
String? vs non-null - Safe call: s?.length
len = s?.length ?: 0
String.
- Not-null assertion: - Smart casts: if (x is
s!!.length String) { /* x is String
(dangerous) here */ }
Kotlin: Functions, Lambdas &
Extensions
Functions, lambdas & extensions:
- Extension functions add functions to existing types at compile-time.
Slide 18 - Kotlin: Interop - Calling
Java from Kotlin
Calling Java from Kotlin example:
Kotlin usage:
- Kotlin treats platform types as potentially nullable; handle accordingly.
Language Differences & Design
Choices
Checked Exceptions: Java enforces checked exceptions; Kotlin does not.
Null safety: Kotlin has nullable types; Java relies on annotations/tooling at best.
Boilerplate: Kotlin reduces boilerplate (data classes, properties, type inference).
Generics: Java uses type erasure; Kotlin can use reified with inline.
Concurrency: Java: threads, reactive frameworks; Kotlin: coroutines + structured concurrency.