Kotlin
Kotlin
Development
Kotlin
• However, for a one line function like this you can directly
assign it and it will "infer" the type
Kotlin: Classes
• Kotlin is designed to remove a lot of the typical "boilerplate"
code from your classes, I.e. getters, setters
• This single line of code will create a simple Java Object with
two properties a firstName and an email
• It has a single constructor which requires both properties
The Same Class in Java
Kotlin: Encapsulation 1
• You may have noticed that the Kotlin code doesn't have
"private"
• In fact it doesn't matter, there are "hidden" getters and
setters being created and used by Kotlin behind the scenes
• We access the properties like this, instead of using getters:
Kotlin: Encapsulation 2
• Okay but that raises the question, what if we want some type
of validation for setting a property?
• We can override the hidden "setter" in our class file (we still
access the value using the "." instead of a setter)
• Note that if we do not include this check, our app will crash if
we haven't called "findViewById" before
Kotlin Features: SAM Conversions
• We use SAM (Single Abstract Method) interfaces all the time
in Android (our onClickListener is one of these)
• Kotlin allows you to pass a "lambda" instead which cleans up
the code
• A lambda is a special function which is not declared but
treated as an expression
• You will use them with the "->" arrow notation, similar (but
not the same) as what you may have done in JavaScript
Lambda onClickListener
• Compare the Java vs. The Kotlin for an onClickListener
Conclusion
• This has just been a very brief overview of what Kotlin offers
• If you are interested in learning more check out the following
resources:
• https://developer.android.com/kotlin
• https://kotlinlang.org/docs/reference/
• It is important to note that Kotlin doesn't "do" anything that
Java couldn't already do, most of the benefits come from
improving readability and reducing potential of bugs