Home > Articles

Writing Kotlin-Friendly Java Code Best Practices

  • Print
  • + Share This

Calling Java code from Kotlin works seamlessly in nearly all cases. Still, there are some practices you can follow to avoid special cases of conflict and aid interoperability even further.

Save 35% off the list price* of the related book or multi-format eBook (EPUB + MOBI + PDF) with discount code ARTICLE.
* See informit.com/terms

From the book
  • Add appropriate nullability annotations to all nonprimitive return types, parameters, and fields to avoid ambiguous platform types.
  • Use the accessor prefixes get, set, and is so that they are automatically accessible as properties from Kotlin.
  • Move lambda parameters to the end of signatures to allow for the simplified syntax where you can pull the lambda out of the parentheses.
  • Do not define methods that can be used as operators from Kotlin unless they indeed make sense for the corresponding operator.
  • Do not use identifiers in Java that are hard keywords in Kotlin to avoid escaping.

Learn more about Kotlin with Kotlin for Android Development.

  • + Share This
  • 🔖 Save To Your Account