Java and Kotlin: A Practical comparison (part I)
Tags: java, kotlin, backenddevelopment
Published: 2023-11-25 22:57:35

### Brief History of Kotlin

Kotlin was developed by **JetBrains** in 2010 and released in 2016. JetBrains open-sourced the project under the Apache 2 license in 2012, and Kotlin v1.0 was **released on 2016**.
The company behind IntelliJ IDEA, needed a language that was **concise, elegant, expressive, and interoperable with Java**, as most of their products were developed in Java. They aimed to reduce the amount of boilerplate code. As a result, although Kotlin is less powerful than Scala, it is much simpler and more importantly, communicates easily with Java code.
**Spring Framework 5.0** introduced dedicated Kotlin support and it is the **recommended language for Android** development since 2017.

### Language Philosophy

- Purely Object-oriented with functions as first class citizens.
- Safety through null-types and immutability.
- Extensibility through extension functions.
- Good tooling support.

It allows a **gradual migration from Java**. That does not happen with Scala.

### Object Orientation

<u>Java code</u>
``` java
public class Person {

    private final String name;
    private int age;

    // Constructor with name and age
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Constructor with name only
    public Person(String name) {
        this(name, 0); // Default age is 0
    }

    public void plus(int lifetime) {
        this.age += lifetime;
    }

    // Getters
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    
    // Factory method
    static Person newBorn(String name) {
        return new Person(name);
    }
}
```

<u>Kotlin code</u>
``` kotlin
data class Person(val name: String,
             var age: Int = 0) {

    companion object {       // Factory method
        fun newBorn(name: String) = Person(name)
    }
}
```

- **Default arguments** allow to have only one constructor in Kotlin. 
- Kotlin has no primitives. <u>Everything</u> is an Object.
- Every Kotlin method has a return type, which can be inferred. If the method is *void*, the return type is *Unit*. This is taken from Scala.

- An *object* in Kotlin is a singleton. The functions defined inside are equivalent to static Java methods.
- A *data* class generates the corresponding getters (and setters for mutable fields), as well as the *hashCode*, *toString* and *equals* methods. Also a <u>*copy*</u> method.
This is taken from *case class* in Scala. 

``` kotlin
val alice = Person("Alice", 42)

alice == Person("Alice", 42)  // Equality by value, not by reference
res1: true
```

- This *copy* method is not generated by the Lombok annotations.
- Named arguments allows to use any parameters order
``` kotlin
val john = alice.copy(name = "John")
```

- <u>Destructuring</u>, like in Javascript and in Python
``` kotlin
val (name, age) = alice   
```

A **record class**, introduced in the latest Java versions, generates most of the methods shown above. The final code is almost equivalent to that obtained with *Lombok* annotations.  

<u>Java 14 and higher</u>

``` java
public record Person(String name, int age) {

    public Person(String name) {
        this(name, 0); // Default age is 0
    }
    
    static Person newBorn(String name, int age) {
        return new Person(name, age);
    }
}
```
``` java
final var alice = new Person("Alice", 42)
alice == new Person("Alice", 42)  

// copy() does not exist

// destructuring does not exist either

// The new keyword is required.
// The semicolons are required.
```

<u>NOTE</u>: this article is the first one of a long series comparing Java and Kotlin. Please stay tuned for the next part.
I will try to post once a week.