Java to Kotlin Conversion Guide
This guide describes our process for converting Java code to Kotlin.
Why Convert to Kotlin?
Java and Kotlin are compatible languages, but we decided to convert our codebase to Kotlin for the following reasons:
- Kotlin is more concise and expressive than Java.
- Kotlin has better support for null safety.
- Kotlin has a number of modern language features that make it easier to write maintainable code.
See our ADR-0001 for more information.
How to Convert Java Code to Kotlin
- Write tests for any code that is not adequately covered by tests.
- Use the “Convert Java File to Kotlin File” action in IntelliJ or Android Studio to convert the Java code.
- Fix any issues that prevent the code from compiling after the automatic conversion.
- Commit the changes as separate commits:
- The change of file extension (e.g.
example.java
->example.kt
). - The conversion of the Java file to Kotlin.
- This can be automated by IntelliJ/Android Studio if you use their VCS integration and enable the option to commit changes separately.
- The change of file extension (e.g.
- Refactor the code to improve readability and maintainability. This includes:
- Removing unnecessary code.
- Using Kotlin’s standard library functions, language features, null safety and coding conventions.
Additional Tips
- Use
when
expressions instead ofif-else
statements. - Use
apply
andalso
to perform side effects on objects. - Use
@JvmField
to expose a Kotlin property as a field in Java.