What I thought about when I started migrating from Java to Kotlin

Started writing Kotlin

I have written Android apps developed by the company in Java so far, but with the official support of Android Studio 3.0 release = Kotlin at the end of October, I decided to use Kotlin to implement the new development part. Also, the existing implementation part is being rewritten little by little as needed (also for practice).

So, I haven't used it for a month yet, but I will write about what I felt when I wrote Kotlin.

Thank you for the existence of the option

There is no need to write it again, but this is what I will mention.
Compared to Swift, which is also used when writing native apps, I was worried that I had to write while paying attention to the occurrence of slimy, but this has been resolved. Of course, you can avoid it by checking Null or using ʻOptional.ofNullable` in Java, but it's good to have a world where Nullable and NotNull are clearly separated.

I like to use the Elvis operator to write Swift's guard.

sample.kt


val str: String = optionalStr ?: return

In order not to erase the goodness of the option, care is also required on the Java side

When dealing with a project in which Java and Kotlin are mixed, there are of course cases where Java code is called from Kotlin. In such a case, if you add annotations on the Java side, the optional restrictions in Kotlin will come into effect.

For example, suppose you have the following code on the Java side.

AppUtil.java


public class AppUtil {
    public static String getVersion() {
        return "1.0";
    }
}

When called from Kotlin side, the return value of ʻAppUtil.getVersion ()will beString!, And it will be in a state where both String and String? are allowed. If you inadvertently enter even null, you will return to the Nullpo era. This will lose its character, so add the @NonNull or @Nullableannotation to the method on the Java side. The IDE will now infer that the return type isString or String? `.

extension is convenient

You can create new methods and properties in an existing class. When I was writing in Java, it was inconvenient to not have this compared to Swift. (To achieve this, there were quite a few static methods of classes like HogeUtil)

Extension.kt


fun Button.disableTouch() {
    alpha = 0.5f
    isEnabled = false
}

The above is an example of adding a method to ʻandroid.widget.Button`.

Static methods and static variables in Java etc.

My favorite static methods and properties don't exist in Kotlin. For example, how to express a factory method like public static HogeFragment newInstance (...) {...} that I usually wrote in the fragment file is to use Companion Object as one of the solutions. There is a hand.

HogeFragment.kt


class HogeFragment : Fragment() {
    companion object {
        fun newInstance(...): HogeFragment {
            ...
        }
    }
}

HogeFragment.kt


companion object {
    @JvmStatic
    fun newInstance(...): HogeFragment {
        ...
    }
}

On the other hand, as a method of expressing static methods (poor things), there is also a way to use top-level functions of files.

HogeFragment.kt


fun topLevelFun(...) {
    ...
}
 
class HogeFragment : Fragment() {
    ...
}

I was wondering if the former method (the one that uses Companion Object) is basically good because it can be called in the same way as in Java and there is no need to worry about name collisions. However, I recently read the following blogs, etc., and learned somehow with an image of how to use them properly. companion object vs. top-level - hydrakecat’s blog

The same applies to static variables, and there are two ways to declare properties in Companion Object and one at the top level. (For the former, you can call it in the same way as in Java by adding @JvmField)

end

So, I haven't started using it yet, but I wrote a little about what I felt when I wrote Kotlin.
In addition to what I've written above, I'm also happy that it's similar in writing to Swift and the amount of code is reduced compared to Java. I think it may be used on the server side.

[PR] I'm looking for a companion

We are looking for friends to write Kotlin, Swift, Rails and Python together in Atrae! https://www.green-japan.com/company/172

Recommended Posts

What I thought about when I started migrating from Java to Kotlin
Summary of points I was worried about when migrating from java to kotlin
What I did when I converted java to Kotlin
Precautions when migrating from VB6.0 to JAVA
What I thought when I started working as an engineer
What I researched about Java 6
What I researched about Java 9
What I researched about Java 7
What I learned about Kotlin
What I researched about Java 5
How to write and notes when migrating from VB to JAVA
ClassCastException occurs when migrating from Java7 to Java8 ~ Generics and overload ~
What I researched about Java learning
Notes on building Kotlin development environment and migrating from Java to Kotlin
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
What I learned from Java monetary calculation
Memo for migration from java to kotlin
What I thought when passing the user input value to the Service class
Run R from Java I want to run rJava
Migrate from Java to Server Side Kotlin + Spring-boot
Getting started with Kotlin to send to Java developers
I want to write quickly from java to sqlite
What Java inexperienced people did to study Kotlin
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] I want to calculate the difference from the date
Behavior when calling Java variadic methods from Scala / Kotlin / Java
From Java to Ruby !!
What I got from continuing to develop open source
What I learned when building a server in Java
I want to transition screens with kotlin and java!
I tried to find out what changed in Java 9
When I started ansible + docker now, I stumbled from the beginning, but I managed to start it
11 items I don't know if it's good to know about JAVA
What I was addicted to when introducing the JNI library
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[Opens aml] NoClassDefFoundError occurs when migrating from Tomcat to weblogic
I want to implement various functions with kotlin and java!
Notes on character encoding when migrating from windows to Mac
About smart cast in callback from Fragment to Activity in Kotlin
[Java] I thought about the merits and uses of "interface"
Migration from Cobol to JAVA
New features from Java7 to Java8
Migrating from vargrant to docker
Connect from Java to PostgreSQL
[Swift] I thought about compare
From Ineffective Java to Effective Java
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
How to deal with the type that I thought about writing a Java program for 2 years
I can't log in to MySQL from Django when using docker-compose
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
I tried to translate the error message when executing Eclipse (Java)
What I was addicted to when implementing google authentication with rails
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned from doing Java work with Visual Studio Code