Let's migrate to make Java more comfortable

This is an article to tell you that I was very happy with the transition from Java to Kotlin.

Java drawbacks

I used to program in Java a while back. But Java has its drawbacks. This is, for example:

: warning: This is an article about migrating from Java. JavaScript is a completely different language. Please be careful. NullPointerException Null may be returned as a return value from the API, or null may be passed as an argument. The former can be avoided by using the Optional class added in Java 8, but the latter cannot be done. You can get run-time errors, but unfortunately you don't get compile errors.

Redundant description

Type description

When declaring a variable, describe the type such as List <String> words. However, it would be nice if we could infer it. Now you can use var, but not for instance fields etc.

About object orientation

In Java, you need to write getters / setters for each encapsulation, which are a bit verbose. Class constructors and overloads 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's still hard to read.

Things to consider when migrating

Things to remember

If the grammar was completely different when you migrated the language, it would be unfamiliar. It may be dynamic, or it may be functional programming.

Being able to migrate little by little

If the new destination programming language is completely incompatible, it will take a lot of time. If possible, I would like you to use the existing Java programs.

That's right, Kotlin!

null safe

Kotlin separates types that can be null and types that can't. String? May contain null. String cannot contain null.

Concise description

In Java, the following classes

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("The argument is 0 or less. Specified value:" + ink);
        this.ink = ink;
    }
}

In Kotlin

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

Simply call. If the setter is unchecked,

class Pen(var ink:Int)

Only. If you follow the naming conventions getXXX and setXXX, you can access it like a field like pen.ink. By the way, getters / setters are generated at compile time for the fields defined earlier in Kotlin.

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

Kotlin can coexist with Java

In fact, Kotlin is converted to a class file when compiled. In other words, it runs on the JVM like Java. Kotlin and Java can coexist, and Kotlin can use Java libraries, inherit from them, 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 / paste), which is very helpful. Languages that run on the JVM include Scala, Groovy, and Ruby (jRuby), but I'm not very familiar with the syntax in Ruby, Scala is slow to compile, and Groovy chose Kotlin because it's dynamically attached. Now, let's go ** to ** migrate ** to Kotlin ** (puns).

Recommended Posts

Let's migrate to make Java more comfortable
Make Java code coverage more comfortable with Jacoco 0.8.0
Java --How to make JTable
How to make a Java container
How to make a Java array
Interface Try to make Java problem TypeScript 7-3
How to make a Java calendar Summary
Easy to make Slack Bot in Java
How to make a Discord bot (Java)
[Java] Beginners want to make dating. 1st
Let's study Java
I tried to make Basic authentication with Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Migrate from Java to Server Side Kotlin + Spring-boot
How to make duplicate check logic more readable
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
String operation Try to make Java problem TypeScript 9-3
[Java] Introduction to Java
Introduction to java
[Small story] I tried to make the java ArrayList a little more convenient
Easy to make LINE BOT with Java Servlet
[Java] How to make multiple for loops single
Initialization of for Try to make Java problem TypeScript 5-4
How to make Java unit tests (JUnit & Mockito & PowerMock)
How to organize information to make programming learning more efficient
[Java basics] Let's make a triangle with a for statement
Let's make a robot! "A simple demo of Java AWT Robot"
I tried to make a login function in Java
I used to make nc (netcat) with JAVA normally
[Introduction to Android application development] Let's make a counter
Make Blackjack in Java
Let's touch on Java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
[Java] Connect to MySQL
Let's make Rails-like (View)
Kotlin's improvements to Java
How to make shaded-jar
From Java to Ruby !!
Let's scrape with Java! !!
Introduction to java command
Let's write how to make API with SpringBoot + Docker from 0
Let's make a calculator application in Java ~ Display the application window
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I tried to make Java Optional and guard clause coexist
I tried to make a client of RESAS-API in Java