Memo for migration from java to kotlin

** Introduction **

Kotlin has become the official language of Android, and it seems that we will develop more with kotlin from now on. I had the opportunity to replace some java classes with kotlin at work Make a note of how you fixed the parts that did not work with batch replacement. Since Android development is still in its first year, please understand that there may be some parts that cannot be reached.

reference

https://www.slideshare.net/kenichitatsuhama/java-kotlin

What i did

** Perform batch replacement **

First, select convert Java File to Kotlin File from the Code menu.

Mostly it will replace it well.

** Correct the description after return @ ~ **

Basic return is used when exiting a method It seems that if it is written in lambda, it will not correctly judge whether to exit the method or lambda. If you type @ after return, the correct one will come out as a candidate.

** Add @JvmStatic to static method **

This is a fix to allow java to access kotlin's static methods.

** Take the member variable m **

If you rename it with the function of the IDE, there is no mistake than manual work, so use it.

** Correct any description of "!!" in the member call **

This will be done even if the member is null, so fix it. There are the following methods.

** Modify so that variables with "!!" are not passed as function arguments **

Before correction.kt


hoge.fuga(piyo!!)

Revised.kt


piyo?.let {
hoge.fuga(it)
}

** Change to lazy property where @BindDrawable is used **

Example.kt


internal val icon: Drawable? by lazy {
ResourcesCompat.getDrawable(resources, R.drawable.icon, null)
}

** Explicitly specify and convert **

Before correction.kt


bundle.getLong(hogehoge) != 0)

Revised.kt


bundle.getLong(hogehoge) != 0L)

** Change var to val where possible **

I think that it is possible to take measures such as delay initialization using the lazy property for parts that are initialized by onCreate etc.

** Internal will be fully published in the same module, so make it private if you don't need it **

internal -> private

** Change as to as? If necessary **

The part where findFragmentByTag is done, etc.

** Singleton is not automatically converted **

Replace with a singleton using the object keyword

** Unused parameters in lambda expressions are renamed or removed with "_" **

We also refactored this time.

at the end

There seems to be other corrections, but that's all for this time. In addition, I will update it when new corrections come out.

Recommended Posts

Memo for migration from java to kotlin
Migration from Cobol to JAVA
From Java to VB.NET-Writing Contrast Memo-
Changes from Java 8 to Java 11
Sum from Java_1 to 100
(Memo) Java for statement
Initial settings for rewriting Java projects to Kotlin
New features from Java7 to Java8
Connect from Java to PostgreSQL
Java development for beginners to start from 1-Vol.1-eclipse setup
[Java] Platforms to choose from for Java development starting now (2020)
From Ineffective Java to Effective Java
[Personal memo] Frequently used Java grammar updated from time to time
[Java] Memo for naming class names
Kotlin Class to send to Java developers
Java to be involved from today
[Android] Convert Android Java code to Kotlin
[Java] Conversion from array to List
Java memo
Notes on building Kotlin development environment and migrating from Java to Kotlin
Comparison memo for those who touch Kotlin (Android) from Swift (iOS) 1
kotlin & Java: How to hide the toolbar only for specific fragments
What I thought about when I started migrating from Java to Kotlin
Kotlin Class part.2 to send to Java developers
[Kotlin] 3 ways to get Class from KClass
Introduction to java for the first time # 2
Convert all Android apps (Java) to Kotlin
About the procedure for java to work
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Introduction to kotlin for iOS developers ⑥-Kotlin creation
Directory change monitoring method memo (Java, Kotlin)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Interoperability tips with Kotlin for Java developers
Introduction to monitoring from Java Touching Prometheus
Java, for statement / while statement starting from beginner
Precautions when migrating from VB6.0 to JAVA
Type conversion from java BigDecimal type to String type
Java code that cannot be used from Kotlin (for in-house study sessions)
Kotlin vs. Java: Which Programming Language to Choose for Your Android App
java anything memo
[Java] How to test for null with JUnit
[Java] From two Lists to one array list
Kotlin functions and lambdas to send to Java developers
Specify Java / Kotlin compile-time options for Android apps
Run R from Java I want to run rJava
Java Silver memo
For JAVA learning (2018-03-16-01)
Migration from Eclipse to IntelliJ (on the way)
Introduce Kotlin to your existing Java Maven Project
Connect to Aurora (MySQL) from a Java application
java, maven memo
Summary of points I was worried about when migrating from java to kotlin
To become a VB.net programmer from a Java shop
How to get Class from Element in Java
Java SE 7 memo
MySQL migration memo (1)
2017 IDE for Java