A summary of what Java programmers find when reading Kotlin source for the first time

I completely forgot because I had been touching Kotlin for the past two months, but when a warrior (programmer) with Java eyes read Kotlin for the first time, it was quite ** slippery **. No matter how great Kotlin has Java integration, if you can't read it in the usual way, you tend to think, "It looks difficult! I like Java that I'm used to!".

Here, I will summarize the ** elements that make your eyes slippery when reading Java when reading Kotlin. It may be useful for anyone reading Kotlin code from now on.

Variable declaration is variable name: type name

Java


Int x = 0;
SomeClass method1(int i, String s, Date date){ ... }

Kotlin


val x: Int = 0
fun method1(i: Int, s: String, date: Date) : SomeClass { ... }

It's the so-called Scala style. Swift also writes this way. People who are accustomed to C and Java will find it difficult to read at first glance.

Remedy: ** Get used to **

It is difficult to distinguish between val and var in the variable declaration

Kotlin


val x = 0 //Cannot be reassigned
var y = 2 //Reassignable

x = 1 //Compile error
y = 3 // OK

val (value) cannot be reassigned, var (variable) can be reassigned. It's the same declaration method as Scala, but what you think is ** one letter difference **. As you can see from the code above, it is very difficult to distinguish the code. It's hell if you use Vi.

That said, Android Studio (IntelliJ) defaults to ** var-declared variable names that are always underlined **, so it's easy to tell if you know this rule. It's a good idea to remember that those who need attention (= var) are underlined. I don't know what's going on with other IDEs.

Remedy: ** Determined by the presence or absence of underlining **

The type is not written in the variable

I think many people are allergic to untyped variables. Please be assured. Variables that do not have a type are also typed properly. Since the Kotlin compiler has a type inference function, even if the type declaration is omitted when declaring a variable, the type declaration is automatically supplemented. The important thing here is that it is the compiler ** that supplements the ** type declaration. It doesn't change which type is attached depending on the state at runtime, and you can't write such code. In addition, since the ordinary type inference device returns the smallest candidate type, y is not added with Int? Or Any in the above code. If you say "The initial value is an actual value, but null can be assigned as the process progresses", let's declare that y is Int ?.

Workaround: Look at the right side of the variable definition. The type of the expression on the right side becomes the type of the variable on the left side as it is.

Do not write new in instance declaration

Java


SomeClass obj = new SomeClass(10, "aaa", new Date());

Kotlin


val obj = SomeClass(10, "aaa", Date())

Kotlin does not write new when creating an instance. If you are accustomed to Java, you will be confused because it is determined whether you are using a constructor or getInstance () based on "presence or absence of new before the class name". Since the code becomes simpler as the identifiers decrease, the readability itself seems to be zero. Also (new Nantoka (...)) doesn't have to be enclosed in parentheses.

Class declaration doubles as constructor declaration

Java


class User {
    String name;
    String email;

    public User(String name, String email){
        this.name = name;
        this.email = email;
    }

    ...
}

Kotlin


class User(val name: String, val email: String){
    ...
}

Kotlin's constructor (more precisely, the primary constructor) differs from java in the following ways:

These features help reduce the amount of code, but those familiar with Java, especially those familiar with the "list member variables at the beginning of the class" culture, are likely to be confused.

Workaround: It's a writer's method rather than a reader's method, but it's a good idea to devise line breaks and indentation so that you can read it with the same feeling as in Java.

Kotlin


class User (
    val name: String,
    val email: Stirng
) {
    ...
}

I'm surprised and my eyes flicker

Java


Int result = obj.method1().method2().method3();

Kotlin


val result : Int = obj!!.method1()!!.method2()!!.method3()!!

The code is rather surprising because it is ** the anti-pattern of nullable processing in the middle **, so there is a problem with the code. If this happens when you automatically convert your java code to kotlin, let's rather rejoice, "I'm glad I found this null-unsafe code."


If I come up with other things, I will update them one by one.

Recommended Posts

A summary of what Java programmers find when reading Kotlin source for the first time
[Java] When writing the source ... A memorandum of understanding ①
Learning memo when learning Java for the first time (personal learning memo)
[Android studio / Java] What you don't understand when you touch it for the first time
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
What Java programmers find useful in Kotlin
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
What an inexperienced person is thinking now after a month of self-taught programming for the first time.
Learn for the first time java # 3 expressions and operators
Summary of file reading method for each Java file format
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
[Java] Summary of for statements
Find the number of days in a month with Kotlin
When reading the source code
Modeling a Digimon with DDD for the first time Part 1
[DL4J] Java deep learning for the first time (handwriting recognition using a fully connected neural network)
Think when Rails (turbolinks) doesn't load the page for the first time
How to check for the contents of a java fixed-length string
Spring Boot for the first time
[For beginners] Summary of java constructor
Generics of Kotlin for Java developers
Spring AOP for the first time
Summary: Implementation of a function that switches the logical value when a link is clicked [for own output]
What I don't like when using interface of a function with default arguments in Kotlin from Java
A memo when you want to clear the time part of the calendar
[First Java] Make something that works with Intellij for the time being
The story of releasing the Android app to the Play Store for the first time.
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
Programming for the first time in my life Java 1st Hello World
Touching kotlin for the first time-Enum Classes
What is the best file reading (Java)
What are the updated features of java 13
Find the difference from a multiple of 10
Summary of good points and precautions when converting Java Android application to Kotlin
The story of intentionally using try catch for the first time in my life
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
What I tried when I wanted to get all the fields of a bean
Summary of points I was worried about when migrating from java to kotlin
Sample source code for finding the least common multiple of multiple values in Java
Impressions and doubts about using java for the first time in Android Studio
A story confirming the implementation of the SendGrid Java library when mail delivery fails
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
Rails: A brief summary of find, find_by, where
I tried using Docker for the first time
A brief summary of Bootstrap features for beginners
Summary of Java environment settings for myself [mac]
What surprised the Java shop when writing PHP
Technology for reading Java source code in Eclipse
The story of learning Java in the first programming
Measure the size of a folder in Java
Set the time of LocalDateTime to a specific time
Walls hit by Rspec for the first time
Feel the passage of time even in Java
What is the volatile modifier for Java variables?
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
A collection of simple questions for Java beginners
What I did when I converted java to Kotlin