Home > Articles

Writing Kotlin-Friendly Java Code Best Practices

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.

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.