0% found this document useful (0 votes)
46 views

Kotlin

Kotlin is a programming language that is fully interoperable with Java and can be used to write Android apps. It is designed to improve programmer productivity by features like type inference, simplified syntax for conditionals, functions, classes and null safety. Kotlin code is generally more concise and readable compared to Java. Key features include variable declaration with var or val, conditionals that return values, simplified collection and loop syntax, lambda expressions for interfaces like onClickListeners, and null safety with the ? operator.

Uploaded by

dragonslayer9896
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Kotlin

Kotlin is a programming language that is fully interoperable with Java and can be used to write Android apps. It is designed to improve programmer productivity by features like type inference, simplified syntax for conditionals, functions, classes and null safety. Kotlin code is generally more concise and readable compared to Java. Key features include variable declaration with var or val, conditionals that return values, simplified collection and loop syntax, lambda expressions for interfaces like onClickListeners, and null safety with the ? operator.

Uploaded by

dragonslayer9896
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Advanced Mobile Application

Development

Kotlin

March 23, 2024


What is Kotlin?
• You may have noticed the option to write an app in Kotlin
when we create new apps with Android Studio
• Kotlin is a fully interoperable language with Java and any
app we've made so far can be written in Kotlin
• It is designed with "programmer productivity" features in
mind
• Many believe that this will be the standard native
development language for Android
Kotlin: Variable Declaration
• Declaring a variable is slightly different in Kotlin, you begin
with either the Var keyword or the Val keyword
• Use Var if you expect the value of a variable to be
reassigned, otherwise use Val
Kotlin: Type Inference
• The compiler has built in "type inference", it will attempt to
"guess" what your variable is if you don't give it an explicit
type
• It guesses based on the value of the variable
Kotlin: Conditionals
• The if statement in Kotlin is quite different from what you're
used to, it returns a value
• It can be used in a similar way to the Java ternary operator
I.e. condition ? then : else

• Here myString is automatically assigned either "Larger" or


"Less or equal to", depending on the value of myInt
Kotlin: Collections
• Kotlin Collections behave similarly to their Java counterparts,
but you will generally create either a Set, a Map or a List
• You also specify whether it should be "mutable" or not, a
mutableList is one that can be changed (add new elements
to it)
Kotlin: Loops
• The For loop in Kotlin resembles a for each loop, it operates
on some "Iterable" object
Kotlin: Functions
• Functions begin with the fun keyword, the "type" for any
parameters must be specified

• This example also shows another nice feature of Kotlin:


String Interpolation, the value of name is used directly
without concatenation
Kotlin: Functions 2
• For more advanced functions you should specify a return
type, if you are returning a value

• 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 the set(value) is below one of our properties


(email) and field is a special keyword used by Kotlin
Kotlin Features: Nulls
• One of the most common causes for crashes is a Null
Pointer Exception
• In this class, we often run into issues like this when we try to
call "setText" on a TextView that is currently null
• Kotlin avoids this issue by introducing the ? "safe-call"
Kotlin Features: Nulls in Java
• This "?" safe call operator essentially serves the same
purpose as a null check in Java

• 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

You might also like