[Java] Let’s move to make Java more comfortable

2 minute read

This is an article to tell you that it was very nice to move from Java to Kotlin.

Java drawbacks

I was programming in Java a while back. But Java has its drawbacks. This is something like this:

:warning: This is an article about migrating from Java. JavaScript is a completely different language. Please be careful.

NullPointerException

If null is returned as a return value from the API or null is passed as an argument. The former can be avoided by using the Optional class added in Java 8, but the latter cannot be managed. It's a pity that I get a run-time error, but no compilation error.

Redundant description

Type description

When declaring a variable, describe the type such as List<String> words. But it would be nice if we could reason about it. Now you can use var, but you can’t do that for instance fields.

About object orientation

In Java, you need to write getters/setters for each encapsulation, which are a bit redundant. The class constructor and overloading are pretty tedious.

About functional programming

In Java, when creating a method (higher order function) that receives a function, such as Function<T,R> You need to write a slightly longer type. It has a fixed naming convention, but it is still difficult to read.

Things to consider when migrating

Less to remember

If the grammar was completely different when you moved between languages, it would be hard to get used to. Some things are dynamic, others focus on functional programming.

Being able to move little by little

It will take a huge amount of time if the new target programming language is completely incompatible. I would like you to be able to use existing Java programs if possible.

Yes Kotlin!

null safety

Kotlin separates nullable and non-nullable types. String? may contain null. String cannot contain null.

Brief description

In Java, a class like

public class Pen {
    private int ink;
    
    public Pen(int ink) {
        setInk(ink);
    }

    public int getInk() {return ink;}
    public void setInk(int ink) {
        if (ink <0)
            throw new IllegalArgumentException("Argument is less than or equal to 0. Specified value: "+ ink);
        this.ink = ink;
    }
}

In Kotlin,

class Pen(ink: Int = 0) {
    var ink:Int = ink
       set(value) {
            if (ink <0)
                throw IllegalArgumentException("Argument is less than 0. Specified value: ${value}")
            field = value
        }
}

And simply call. If the setter is not checked,

class Pen(var ink:Int)

Only. If you follow the naming rules getXXX and setXXX, you can access them like fields like pen.ink. By the way, getter/setter is generated at compile time for the field defined earlier in Kotlin.

The variable type can omit the :Int part if it is initialized together. When it comes to functional programming, it’s as easy as Haskell with String -> Int.

Kotlin can coexist with Java

Actually, Kotlin is converted into a class file when compiled. In other words, it runs on the same JVM as Java. Kotlin and Java can coexist and Kotlin can use Java’s libraries, inherit, and vice versa.

At the end

I use an IDE called IntelliJ. It has a function to convert Java to Kotlin (existing files and copy and paste), which is very helpful. There are languages such as Scala, Groovy, and Ruby (jRuby) that run on the JVM, but in Ruby etc., I can not adapt to the syntax very much, Scala has a slow compilation speed, and Groovy chose Kotlin because it is dynamic. Now, everyone will move to Kotlin let’s go (damaged).