Kotlin vs. Java: Key Differences

Tutorial 3 of 5

Kotlin vs. Java: Key Differences

1. Introduction

Welcome to this tutorial on the key differences between Kotlin and Java. Both of these languages are used for Android development and are interoperable, but they have significant differences that you need to be aware of when writing code.

What You Will Learn

  • The key differences between Kotlin and Java
  • How these differences affect the way you write code
  • Best practices when using both languages

Prerequisites

  • Basic understanding of programming concepts
  • Familiarity with both Java and Kotlin is helpful but not required

2. Step-by-Step Guide

Let's dive into the differences between Kotlin and Java.

Null Safety

In Java, accessing a null reference will result in a NullPointerException. Kotlin solves this problem by distinguishing nullable and non-nullable types.

// Java
String str = null;
System.out.println(str.length()); // NullPointerException
// Kotlin
var str: String? = null
println(str?.length) // null

Extension Functions

Kotlin allows you to extend a class with new functionality without having to inherit from the class. You can't do this in Java.

// Kotlin
fun String.shout() = this.uppercase()

println("hello".shout()) // Output: HELLO

Coroutines

Kotlin has built-in support for coroutines, making asynchronous programming easier. Java can also handle asynchronous programming but it's more complex and verbose.

// Kotlin
suspend fun doSomething() {
    delay(1000L)
    println("Hello")
}

fun main() = runBlocking {
    launch { doSomething() }
}

3. Code Examples

Null Safety Example

// Java
String str = null;
if (str != null) {
    System.out.println(str.length());
} else {
    System.out.println("Null string");
}
// Kotlin
var str: String? = null
println(str?.length ?: "Null string")

Both examples handle null values, but Kotlin does it with fewer lines of code and is more readable.

Extension Functions Example

// Kotlin
fun String.addHello() = "Hello, $this"
println("John".addHello()) // Output: Hello, John

In this Kotlin example, we add a new function to the String class that prepends "Hello, " to the current string.

4. Summary

We've covered the key differences between Kotlin and Java, including null safety, extension functions, and coroutines.

Keep practicing and experimenting with both languages to get a solid understanding of their differences. Here are some additional resources:
- Kotlin Documentation
- Java Documentation

5. Practice Exercises

  1. Write a Kotlin extension function for the Integer class that doubles the value.
  2. Convert the following Java code to Kotlin and handle possible null values:
String name = null;
System.out.println(name.length());
  1. Write a Kotlin coroutine that delays for 1 second, then prints "Hello, World!"

Solutions:

fun Int.double() = this * 2
println(3.double()) // Output: 6
var name: String? = null
println(name?.length ?: "Null value")
suspend fun greet() {
    delay(1000L)
    println("Hello, World!")
}

fun main() = runBlocking {
    launch { greet() }
}