SlideShare a Scribd company logo
1 of 25
Download to read offline
From Java to Kotlin
lessons learned converting the Android rentals owner app
Android rentals owner app
Vacation Rentals Owner App by TripAdvisor

Easy to manage your TripAdvisor vacation rental.
All of the important features on your owner dashboard are now available in your pocket.
Confirm bookings, send quotes, and respond to guests from wherever you are.
Update your calendar in a snap.
Quickly see who is checking in next, and when your payment arrives.
Take photos and upload them to your listing
Kotlin highlights
Conciseness
You’ll notice Java files consistently shrinking.

Drastically reduce the amount of boilerplate code.
Safety
Avoid entire classes of errors such as null pointer exceptions.
Interoperability
Kotlin is fully compatible and 100% interoperable with Java

You can keep using your favourite Java libraries.
Compatibility
Kotlin is fully compatible with JDK 6 and can run on older Android devices.

Generates the same byte code as your Java code.
Easy to migrate to
Easy, gradual, step by step migration path of large codebases.

You can start writing new code in Kotlin while keeping older parts of your system in Java.
Tooling
Improved standard library.

Stay on your familiar technology stack while reaping the benefits of a more modern language.
Learning Curve
For a Java developer, getting started with Kotlin is very easy.
Show me the code
⌘ + ⌥ + Shift + K
Show me the Kotlin code
Class and method declaration
Classes are declared with class

By default, all classes in Kotlin are final, use open to allow inheritance

There is no implements and extends

To declare an explicit supertype, place the type after a colon in the class header

Functions are declared with fun

Kotlin requires explicit annotations for overridable

Argument types are suffixed as opposed to Java where they are prefixed

Function arguments are always immutable

Companion Objects
In Kotlin, unlike Java or C#, classes do not have static methods

You can write static elements as a member of an object declaration inside that class

And there is that funny looking question mark
We’ll get there in a few minutes …
Variables and types
Use var

To declare mutable variables, i.e. you can re-assign values to it

Use val

To declare immutable variables, i.e. you cannot re-assign values to it

Use const val
To identify compile-time constants, i.e. its value is assigned at compile time

Visibility Modifiers
Similar to Java with:

public, private, protected and internal for module level

Basic Types
Unlike Java, all types in Kotlin are objects, i.e. there are no primitive types

Type inference
There is no need to declare explicit types as it’s inferred from the context
String templates
Use string templates
In Kotlin strings may contain template expressions

A template expression starts with a dollar sign ($) inside the string

Use $myName for simple name and ${myName.length} for expressions

No need for String.format(), StringBuilder() or string concatenation
Single Abstract Method conversion
Use lambdas for SAM types
SAM type is any interface with a single method

Like Java 8 you can use a lambda as an implementation for a SAM type

On Android, this can potentially replace a lot of anonymous classes
Null safety … remember the question mark?
Type declarations are non-nullable by default
You’ll get a compile-time error if you try to set them to a null value

Use the question mark for nullable types
More like the Java style reference that can hold a null value

e.g. type String would be non-nullable and String? nullable

Likewise, the compiler will complain on non-safe operations on a nullable type
Biggest offender for boilerplate code in Java
⌘ + ⌥ + Shift + K
Kotlin removes all the boilerplate code
Need equals, hashCode, toString and clone?
⌘ + ⌥ + Shift + K
Use a data class variant
Class extensions instead of utility class in Java
Utility class in Java
⌘ + ⌥ + Shift + K
Method and properties as class extensions
Class extensions
extend a class with new functionality without having to inherit from it
Take advantage of class extensions in Kotlin
Avoid the cost of conversion
Our app crashed more than usual
after releasing ~20% of the code converted
and is now crashing less than ever
Return value of calls to Java might be null
Consider the following Java function
it could return null if the result of getIntent.getString(intentKey) is null
If it returns null, the following call from Kotlin

throws an java.lang.IllegalStateException as the non-nullable type String is inferred
Make the type nullable
remove the type inference, add a question mark to the type to make it nullable and handle the null
… and the same goes for extending Java classes
Consider the following Kotlin code
from a class that extends Google’s GcmListenerService writen in Java
Turns out the from argument can be null on some Android 4.x versions. Who would have thought?

you’ll get a java.lang.IllegalArgumentException as it’s set as a non-nullable String type
Like before, change the type to a nullable one
changing from String type to nullable String? type would avoid this issue
Type casting null values
Type casting a null value
common use case when type casting objects from intents or layout elements in Android
Make the type nullable
just add a question mark to the type and handle the null
Will throw a runtime kotlin.TypeCastException if the value is null

and it tries to cast it to a non-nullable type
Make good use of @Nullable and @NotNull annotations in Java
helps converting to nullable or non-nullable types in Kotlin
Access to uninitialised lateinit properties
Be careful about promising a variable will be initialised
as the following will throw a kotlin.UninitializedPropertyAccessException at runtime
More suited for classes where you don’t have access to the constructor
a good example are Android activity classes and initialise the var as early as possible
Think about the process
Keep file history
Conversion within the Android Studio or IntelliJ is as simple as ⌘+⌥+Shift+K, but …

… it actually deletes the .java file and creates a new .kt file

You can always checkout an older branch or tag but, …

… keeping history from the .java file changes is usually more helpful than harmful

Consider alternative approaches if you want to keep history
Don’t rely on the automatic conversion only
Always manually review the conversion

Remove rabbit ears (!!) notations: if it’s supposed to be null why are you guaranteeing it’s not? 

Convert unit tests along side the code they’re meant to test.

Yes, you can convert or write tests using Kotlin with JUnit, TestNG, etc …
Start by converting non-critical flows
To minimise impact on you application start with a now critical flow

Resist refactoring!

Focus on conversion and address any refactoring after conversion is proven stable
Plan ahead and across teams
Frequently conversion bugs affect UX even if they don’t crash the app
Reap the benefits
Improved app stability
NPEs affect your app performance and stability and at scale, they can have a huge impact.
Improved productivity
Less time writing boilerplate code.

Code reviews decrease in size and the compiler is already taking care of things like NPE checks.
Happy engineers
Great, robust and easy to maintain code

More Related Content

What's hot

Eclipse Tips & Tricks - EclipseCon North America 2014
Eclipse Tips & Tricks - EclipseCon North America 2014Eclipse Tips & Tricks - EclipseCon North America 2014
Eclipse Tips & Tricks - EclipseCon North America 2014Noopur Gupta
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
What's coming to c# (Tel-Aviv, 2018)
What's coming to c# (Tel-Aviv, 2018)What's coming to c# (Tel-Aviv, 2018)
What's coming to c# (Tel-Aviv, 2018)Moaid Hathot
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScriptbinDebug WorkSpace
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6BlackRabbitCoder
 
C++ to java
C++ to javaC++ to java
C++ to javaAjmal Ak
 
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift ApplicationsHack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applicationseightbit
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryKnoldus Inc.
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noiseNeeraj Bhusare
 
Eclipse Luna - What's New!
Eclipse Luna - What's New!Eclipse Luna - What's New!
Eclipse Luna - What's New!Noopur Gupta
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: FundamentalsMahmoud Abdallah
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design GuidelinesMohamed Meligy
 

What's hot (20)

Eclipse Tips & Tricks - EclipseCon North America 2014
Eclipse Tips & Tricks - EclipseCon North America 2014Eclipse Tips & Tricks - EclipseCon North America 2014
Eclipse Tips & Tricks - EclipseCon North America 2014
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
What's coming to c# (Tel-Aviv, 2018)
What's coming to c# (Tel-Aviv, 2018)What's coming to c# (Tel-Aviv, 2018)
What's coming to c# (Tel-Aviv, 2018)
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScript
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
C++ to java
C++ to javaC++ to java
C++ to java
 
Kotlin
KotlinKotlin
Kotlin
 
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift ApplicationsHack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java Library
 
Switch statement
Switch statementSwitch statement
Switch statement
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
Eclipse Luna - What's New!
Eclipse Luna - What's New!Eclipse Luna - What's New!
Eclipse Luna - What's New!
 
F# for Scala developers
F# for Scala developersF# for Scala developers
F# for Scala developers
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: Fundamentals
 
Scala’s implicits
Scala’s implicitsScala’s implicits
Scala’s implicits
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design Guidelines
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 

Similar to From Java to Kotlin

Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJDSCBVRITH
 
MOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxMOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxkamalkantmaurya1
 
Top 10 Tips for Developing Android Apps Using Kotlin
Top 10 Tips for Developing Android Apps Using KotlinTop 10 Tips for Developing Android Apps Using Kotlin
Top 10 Tips for Developing Android Apps Using KotlinSofiaCarter4
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional waynluaces
 
Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developertroubledkumi
 
What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.SimileoluwaAluko
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional wayThoughtworks
 
9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlinZoran Stanimirovic
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptxShayantaniKar
 
Kotlin vs Java: Which is Better for Android App Development?
Kotlin vs Java: Which is Better for Android App Development?Kotlin vs Java: Which is Better for Android App Development?
Kotlin vs Java: Which is Better for Android App Development?Marie Weaver
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languagessuser2963071
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentJayaprakash R
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With KotlinGaurav sharma
 
Kotlin vs Java: Choosing The Right Language
Kotlin vs Java: Choosing The Right LanguageKotlin vs Java: Choosing The Right Language
Kotlin vs Java: Choosing The Right LanguageFredReynolds2
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestableRoyKlein
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developersMohamed Wael
 

Similar to From Java to Kotlin (20)

Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJ
 
MOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxMOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptx
 
moocs_ppt.pptx
moocs_ppt.pptxmoocs_ppt.pptx
moocs_ppt.pptx
 
Top 10 Tips for Developing Android Apps Using Kotlin
Top 10 Tips for Developing Android Apps Using KotlinTop 10 Tips for Developing Android Apps Using Kotlin
Top 10 Tips for Developing Android Apps Using Kotlin
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional way
 
Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developer
 
What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
 
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor BuzatovićJavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
 
9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptx
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Kotlin vs Java: Which is Better for Android App Development?
Kotlin vs Java: Which is Better for Android App Development?Kotlin vs Java: Which is Better for Android App Development?
Kotlin vs Java: Which is Better for Android App Development?
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
Getting Started With Kotlin
Getting Started With KotlinGetting Started With Kotlin
Getting Started With Kotlin
 
Kotlin vs Java: Choosing The Right Language
Kotlin vs Java: Choosing The Right LanguageKotlin vs Java: Choosing The Right Language
Kotlin vs Java: Choosing The Right Language
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Testing the untestable
Testing the untestableTesting the untestable
Testing the untestable
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 

Recently uploaded

Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfCWS Technology
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...nishasame66
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsChandrakantDivate1
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesChandrakantDivate1
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsChandrakantDivate1
 

Recently uploaded (6)

Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 

From Java to Kotlin

  • 1. From Java to Kotlin lessons learned converting the Android rentals owner app
  • 2. Android rentals owner app Vacation Rentals Owner App by TripAdvisor Easy to manage your TripAdvisor vacation rental. All of the important features on your owner dashboard are now available in your pocket. Confirm bookings, send quotes, and respond to guests from wherever you are. Update your calendar in a snap. Quickly see who is checking in next, and when your payment arrives. Take photos and upload them to your listing
  • 3. Kotlin highlights Conciseness You’ll notice Java files consistently shrinking. Drastically reduce the amount of boilerplate code. Safety Avoid entire classes of errors such as null pointer exceptions. Interoperability Kotlin is fully compatible and 100% interoperable with Java You can keep using your favourite Java libraries. Compatibility Kotlin is fully compatible with JDK 6 and can run on older Android devices. Generates the same byte code as your Java code. Easy to migrate to Easy, gradual, step by step migration path of large codebases. You can start writing new code in Kotlin while keeping older parts of your system in Java. Tooling Improved standard library. Stay on your familiar technology stack while reaping the benefits of a more modern language. Learning Curve For a Java developer, getting started with Kotlin is very easy.
  • 4. Show me the code ⌘ + ⌥ + Shift + K
  • 5. Show me the Kotlin code
  • 6. Class and method declaration Classes are declared with class By default, all classes in Kotlin are final, use open to allow inheritance There is no implements and extends To declare an explicit supertype, place the type after a colon in the class header Functions are declared with fun Kotlin requires explicit annotations for overridable Argument types are suffixed as opposed to Java where they are prefixed Function arguments are always immutable Companion Objects In Kotlin, unlike Java or C#, classes do not have static methods You can write static elements as a member of an object declaration inside that class And there is that funny looking question mark We’ll get there in a few minutes …
  • 7. Variables and types Use var To declare mutable variables, i.e. you can re-assign values to it Use val To declare immutable variables, i.e. you cannot re-assign values to it Use const val To identify compile-time constants, i.e. its value is assigned at compile time Visibility Modifiers Similar to Java with: public, private, protected and internal for module level Basic Types Unlike Java, all types in Kotlin are objects, i.e. there are no primitive types Type inference There is no need to declare explicit types as it’s inferred from the context
  • 8. String templates Use string templates In Kotlin strings may contain template expressions A template expression starts with a dollar sign ($) inside the string Use $myName for simple name and ${myName.length} for expressions No need for String.format(), StringBuilder() or string concatenation
  • 9. Single Abstract Method conversion Use lambdas for SAM types SAM type is any interface with a single method Like Java 8 you can use a lambda as an implementation for a SAM type On Android, this can potentially replace a lot of anonymous classes
  • 10. Null safety … remember the question mark? Type declarations are non-nullable by default You’ll get a compile-time error if you try to set them to a null value Use the question mark for nullable types More like the Java style reference that can hold a null value
 e.g. type String would be non-nullable and String? nullable Likewise, the compiler will complain on non-safe operations on a nullable type
  • 11. Biggest offender for boilerplate code in Java ⌘ + ⌥ + Shift + K
  • 12. Kotlin removes all the boilerplate code
  • 13. Need equals, hashCode, toString and clone? ⌘ + ⌥ + Shift + K
  • 14. Use a data class variant
  • 15. Class extensions instead of utility class in Java
  • 16. Utility class in Java ⌘ + ⌥ + Shift + K
  • 17. Method and properties as class extensions Class extensions extend a class with new functionality without having to inherit from it
  • 18. Take advantage of class extensions in Kotlin
  • 19. Avoid the cost of conversion Our app crashed more than usual after releasing ~20% of the code converted and is now crashing less than ever
  • 20. Return value of calls to Java might be null Consider the following Java function it could return null if the result of getIntent.getString(intentKey) is null If it returns null, the following call from Kotlin throws an java.lang.IllegalStateException as the non-nullable type String is inferred Make the type nullable remove the type inference, add a question mark to the type to make it nullable and handle the null
  • 21. … and the same goes for extending Java classes Consider the following Kotlin code from a class that extends Google’s GcmListenerService writen in Java Turns out the from argument can be null on some Android 4.x versions. Who would have thought? you’ll get a java.lang.IllegalArgumentException as it’s set as a non-nullable String type Like before, change the type to a nullable one changing from String type to nullable String? type would avoid this issue
  • 22. Type casting null values Type casting a null value common use case when type casting objects from intents or layout elements in Android Make the type nullable just add a question mark to the type and handle the null Will throw a runtime kotlin.TypeCastException if the value is null and it tries to cast it to a non-nullable type Make good use of @Nullable and @NotNull annotations in Java helps converting to nullable or non-nullable types in Kotlin
  • 23. Access to uninitialised lateinit properties Be careful about promising a variable will be initialised as the following will throw a kotlin.UninitializedPropertyAccessException at runtime More suited for classes where you don’t have access to the constructor a good example are Android activity classes and initialise the var as early as possible
  • 24. Think about the process Keep file history Conversion within the Android Studio or IntelliJ is as simple as ⌘+⌥+Shift+K, but … … it actually deletes the .java file and creates a new .kt file You can always checkout an older branch or tag but, … … keeping history from the .java file changes is usually more helpful than harmful Consider alternative approaches if you want to keep history Don’t rely on the automatic conversion only Always manually review the conversion Remove rabbit ears (!!) notations: if it’s supposed to be null why are you guaranteeing it’s not? Convert unit tests along side the code they’re meant to test. Yes, you can convert or write tests using Kotlin with JUnit, TestNG, etc … Start by converting non-critical flows To minimise impact on you application start with a now critical flow Resist refactoring! Focus on conversion and address any refactoring after conversion is proven stable Plan ahead and across teams Frequently conversion bugs affect UX even if they don’t crash the app
  • 25. Reap the benefits Improved app stability NPEs affect your app performance and stability and at scale, they can have a huge impact. Improved productivity Less time writing boilerplate code. Code reviews decrease in size and the compiler is already taking care of things like NPE checks. Happy engineers Great, robust and easy to maintain code