SlideShare a Scribd company logo
1 of 42
Download to read offline
Intro to Kotlin
Android KTX
HELLO!
I am Syed Awais Mazhar
Having 2+ years of experience of development.
Working with Kotlin from couple of months.
What is ‘Kotlin’?
❏ Kotlin (or Kettle, Finnish: Retusaari, Swedish: Reitskär) is a
Russian island, located near the head of the Gulf of Finland,
32 kilometres (20 mi) west of Saint Petersburg in the Baltic
Sea.
❏ A new programming language created by JetBrains,
creators of PhpStorm, WebStorm, & PyCharm etc.
❏ Kotlin is concise, safe, pragmatic, and focused on
interoperability with Java code. It can be used almost
everywhere Java is used today - for server-side
development, Android apps, and much more.
❏ Inspired by Java, Scala, C#, Groovy..
Our Agenda
Java
- Bad parts
Kotlin
- Why?
- Syntax
Kotlin
Extensions
Java - Bad Parts
● Null References
● Raw types
● Missing SAM types (Single Abstract Method)
● Mutability problems
● Missing Lambda expressions + Inline functions = performant
custom control structures
-Although these things Introduced in Java 8 but still it is a procedural language
● As It is 22 years old language, there are thousands of dex
methods needs to be compiled.
● Java is a procedural language.
● Java requires long code combinations to be written
● Java’s syntax is also pretty verbose.
I call it my
billion-dollar mistake.
It was the invention of
the null reference in
1965
~Ton e
Kotlin - Why?
› Statistically typed language for the JVM, Android and the
browser.
› Highly interoperable with Java
› Lightweight (<7000 methods)
› Concise - Avoid boilerplate code.
› Safe - Convert runtime errors into compile time errors.
Null safety guarantees.
› Open source community
› Supports both, OOP & Functional programming approach.
› Very easy to learn
Kotlin - Why?
Kotlin provides us many additional features (1), as
› Null-safety
› Extension functions
› Type inference for variable and property types
› Smart cast
› First-class delegation
› Lambda expressions + Inline functions = performant
custom control structures
Kotlin - Why?
- additional features (2)
› Singletons
› Declaration-site variance & Type projections
› Range expressions
› Companion objects
› Data classes
› Separate interfaces for read-only and mutable collections
fun with
Kotlin
Kotlin - Syntax
- Data type map
Kotlin - Syntax
- Properties: val & var
● val is immutable (read-only) and you can only assign a
value to them exactly one time. This is the recommended
type to use.
● var is mutable and can be reassigned.
● Unit in Kotlin corresponds to the void in Java. Unit is a real
class (Singleton) with only one instance.
● Nothing is a type in Kotlin that represents “a value that
never exists”, that means just “no value at all”
Kotlin - Syntax
- String Templates
● Kotlin supports template expressions
● Simple reference uses $
● Complex references uses ${}
● Raw Strings
*See Code examples
Kotlin - Syntax
- String Templates
Kotlin Java
Kotlin - Syntax
- Null Safety
● No more NPEs
● Possible causes for NPE:
○ !!
○ throw
NullPointerException();
○ External Java code
○ Data inconsistency with
regard to initialization
Kotlin - Syntax
- Visibility Modifiers
Modifier
› public
(default)
› internal
› protected
› private
Class member
› Visible everywhere
› Visible in a module
› Visible in subclasses
› Visible in a class
Top-level declaration
› Visible everywhere
› Visible in a module
› ---
› Visible in a file
Kotlin - Syntax
- Classes
● Many classes in the same file
● Classes and methods are final by default. Use open for
extensibility.
● Only properties(public by default) and functions(fun)
● No new keyword on instantiation
● lateinit for properties (not null) and they should be
mutable
● init block is equal with the constructor in Java
● Kotlin interfaces are similar to those of Java 8: they can
contain definitions of abstract methods as well as
implementations of non-abstract methods , but they can’t
contain any state.
Kotlin - Syntax
- Classes
● inner keyword to be able to access members of the
surrounding class
○ Nested classes don’t reference their outer class, whereas inner classes
do.
*See Code examples
Kotlin - Syntax
- Classes
Kotlin Java
Kotlin - Syntax
- Data Classes | Autogenerated implementations of universal methods
Classes that do nothing but hold data.
If you add the modifier data to your class, all the necessary
methods are automatically generated for you.
Compiler will auto generate functions, i.e. copy() function,
equals() and hashCode() pair, and toString().
To declare a class data class, you must fulfil these.
› The primary constructor must have at least one parameter.
› The class cannot be open, abstract, inner or sealed.
› The class may extend other classes or implement interfaces.
Kotlin - Syntax
- Sealed classes
To avoid an extra ‘else’ branch, while using ‘when’ expression,
Kotlin provided us sealed modifier.
Mark a superclass with the sealed modifier, and that restricts
the possibility of creating subclasses. All the direct subclasses
must be nested in the superclass.
Kotlin - Syntax
- Object classes
Declaring a class and creating an instance, combined, with the
object keyword in kotlin now.
› object declaration is a way to define a singleton.
› companion object can contain factory methods and other methods
that are related to this class but don’t require a class instance to be
called. Their members can be accessed via class name.
› Object expression is used instead of Java’s anonymous inner class.
To use a Kotlin object from the Java code, you will access the
static INSTANCE field. This will allow to access Kotlin’s objects.
*See Code examples
Kotlin - Syntax
- Object classes
Kotlin - Syntax
- Operator Overloading & Infix Functions
● Add operators to the classes by using the operator
modifier.
● Create infix functions with the infix keyword
Infix functions must satisfy the following requirements
○ They must be member functions or extension functions.
○ They must have a single parameter
○ The parameter must not accept variable number of
arguments and must have no default value.
*See Code examples
Kotlin - Syntax
- Operator Overloading
Kotlin - Syntax
- Operator Overloading
Expression
› a * b
› a / b
› a % b
› a + b
› a - b
› ++a, a++
› --a, a--
› !a
Function name
› times
› div
› mod
› plus
› minus
› inc
› Dec
› not
Kotlin - Syntax
- Infix Functions
Extension Functions
Android-Ktx
Extension Functions
Extension functions are normal static methods bearing no
connection with the class they are extending, other than taking
an instance of this class as a parameters.
We can extend any class with new features even if we don’t
have access to the source code The extension function acts as
part of the class.Basically, an extension function is a
member function of a class that is defined outside the class.
› do not modify the original class
› the function is added as a static import
› can be declared in any file
› common practice: create files which include a set of related functions
Extension Functions
Let’s add a utility function in String class, which will return last character of String.
See code below:
An extension function ‘lastChar()’ added in String class.
In variable ‘output’ we will receive the last character of string, using our extension
function.
Android KTX
Provides Kotlin
Extensions for
Easier Mobile
Development
Android-KTX
"Kotlin on Android is here to stay, and we have big plans for it."
Florina Muntenescu
Android developer advocate at Google
The team announced the extensions, named Android KTX, for
"even sweeter Kotlin development for Android."
That sweetness comes by providing more concise code that feels
natural to those working in the Android ecosystem.
The extensions are provided via APIs atop the base Android
framework (available now) and to the Support Library (coming
soon). The core-ktx packages on GitHub now show items ranging
from androidx.animation to androidx.view.
Android-KTX
The extensions provide handy autocomplete coding shortcuts for
many different operations. While sometimes they just save a few
seconds or so by letting coders shorten some small constructs, i.e. It
turned the old way of triggering an action with a View's onPreDraw
callback from this:
Android-KTX
Into this:
● Views
We have some similar functions available for the View class
also. Setting callbacks for layout events is super clean:
The postDelayed method is now available as a function:
Android-KTX
If you need to convert a View instance to a Bitmap, you can do so
with this single line of code!
Updating the padding for a view is now a lot cleaner and easier to
do so.
Android-KTX
● ViewGroup
There are some handy ViewGroup related functions that you’ll
likely be using in your projects! For example, checking a if a
viewgroup contains a view:
Looping through the children of a viewgroup (where it
represents the child):
Android-KTX
● Margins
Similar to setting the padding for View instances, we can now
alter the margins for our layout param instances in a similar
manner with the following functions.
Android-KTX
● Graphics
The Graphics related packages within KTX are pretty beefy. To
begin with we now have some handy functions to easily
perform operations that convert bitmaps to the following
types:
Android-KTX
KTX offers a collection of operations related to Time, Content
Package, OS Package & many new extension functions in Utils.
There’s a collection of extensions related to animations within the
library and many extensions in Cursor Class.
Conclusion is that this is an extensive collection of extension
functions, which will make developer’s life much easy.
You need to explore and use it!
Thank you, Happy
Coding!
Any Questions?
References
› https://adtmag.com/articles/2018/02/06/android-ktx.aspx
› https://medium.com/exploring-android/exploring-ktx-for-
android-13a369795b51
› https://try.kotlinlang.org
› https://kotlinlang.org/docs/books.html
› https://kotlinlang.org/docs/tutorials/koans.html

More Related Content

What's hot

Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show fileSaurabh Tripathi
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin courseGoogleDevelopersLeba
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin XPeppers
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better JavaGarth Gilmour
 
Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Ajinkya Saswade
 
Introduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in KotlinIntroduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in Kotlinvriddhigupta
 
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | Edureka
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | EdurekaKotlin Tutorial for Beginners | Kotlin Android Tutorial | Edureka
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | EdurekaEdureka!
 
Kotlin Jetpack Tutorial
Kotlin Jetpack TutorialKotlin Jetpack Tutorial
Kotlin Jetpack TutorialSimplilearn
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeRamon Ribeiro Rabello
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndreas Jakl
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial Jm Ramos
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | EdurekaEdureka!
 
Jetpack Compose beginner.pdf
Jetpack Compose beginner.pdfJetpack Compose beginner.pdf
Jetpack Compose beginner.pdfAayushmaAgrawal
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutinesRoman Elizarov
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable codeGeshan Manandhar
 

What's hot (20)

Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin course
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
TestNG
TestNGTestNG
TestNG
 
Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI
 
Introduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in KotlinIntroduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in Kotlin
 
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | Edureka
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | EdurekaKotlin Tutorial for Beginners | Kotlin Android Tutorial | Edureka
Kotlin Tutorial for Beginners | Kotlin Android Tutorial | Edureka
 
Kotlin Jetpack Tutorial
Kotlin Jetpack TutorialKotlin Jetpack Tutorial
Kotlin Jetpack Tutorial
 
Kotlin on android
Kotlin on androidKotlin on android
Kotlin on android
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack Compose
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | Edureka
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Jetpack Compose beginner.pdf
Jetpack Compose beginner.pdfJetpack Compose beginner.pdf
Jetpack Compose beginner.pdf
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 

Similar to Intro to Kotlin Android KTX

9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlinZoran Stanimirovic
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentAdam Magaña
 
A short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersA short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersAntonis Lilis
 
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
 
MOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxMOOC_PRESENTATION_KOTLIN[1].pptx
MOOC_PRESENTATION_KOTLIN[1].pptxkamalkantmaurya1
 
Is this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin languageIs this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin languageAntonis Lilis
 
Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language Mobio Solutions
 
Kotlin for Android
Kotlin for AndroidKotlin for Android
Kotlin for AndroidHan Yin
 
Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJDSCBVRITH
 
Kotlin With JetPack Compose Presentation
Kotlin With JetPack Compose PresentationKotlin With JetPack Compose Presentation
Kotlin With JetPack Compose PresentationKnoldus Inc.
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ ProgrammingPreeti Kashyap
 
Building and Deploying Scalable NLP Model Services
Building and Deploying Scalable NLP Model ServicesBuilding and Deploying Scalable NLP Model Services
Building and Deploying Scalable NLP Model ServicesZachary S. Brown
 
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
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 

Similar to Intro to Kotlin Android KTX (20)

9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin
 
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ć
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
 
A short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersA short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developers
 
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
 
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
 
Is this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin languageIs this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin language
 
Kotlin tlv
Kotlin tlvKotlin tlv
Kotlin tlv
 
Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Why Kotlin?
Why Kotlin?Why Kotlin?
Why Kotlin?
 
Kotlin for Android
Kotlin for AndroidKotlin for Android
Kotlin for Android
 
Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJ
 
Kotlin With JetPack Compose Presentation
Kotlin With JetPack Compose PresentationKotlin With JetPack Compose Presentation
Kotlin With JetPack Compose Presentation
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
 
Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
 
Building and Deploying Scalable NLP Model Services
Building and Deploying Scalable NLP Model ServicesBuilding and Deploying Scalable NLP Model Services
Building and Deploying Scalable NLP Model Services
 
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.
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 

More from Syed Awais Mazhar Bukhari (6)

Android App Bundles - Overview
Android App Bundles - OverviewAndroid App Bundles - Overview
Android App Bundles - Overview
 
CDD - Atomic Design Methodology
CDD - Atomic Design MethodologyCDD - Atomic Design Methodology
CDD - Atomic Design Methodology
 
Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
 
Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
 
Methods of data recovery
Methods of data recoveryMethods of data recovery
Methods of data recovery
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Intro to Kotlin Android KTX

  • 2. HELLO! I am Syed Awais Mazhar Having 2+ years of experience of development. Working with Kotlin from couple of months.
  • 3. What is ‘Kotlin’? ❏ Kotlin (or Kettle, Finnish: Retusaari, Swedish: Reitskär) is a Russian island, located near the head of the Gulf of Finland, 32 kilometres (20 mi) west of Saint Petersburg in the Baltic Sea. ❏ A new programming language created by JetBrains, creators of PhpStorm, WebStorm, & PyCharm etc. ❏ Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today - for server-side development, Android apps, and much more. ❏ Inspired by Java, Scala, C#, Groovy..
  • 4. Our Agenda Java - Bad parts Kotlin - Why? - Syntax Kotlin Extensions
  • 5. Java - Bad Parts ● Null References ● Raw types ● Missing SAM types (Single Abstract Method) ● Mutability problems ● Missing Lambda expressions + Inline functions = performant custom control structures -Although these things Introduced in Java 8 but still it is a procedural language ● As It is 22 years old language, there are thousands of dex methods needs to be compiled. ● Java is a procedural language. ● Java requires long code combinations to be written ● Java’s syntax is also pretty verbose.
  • 6. I call it my billion-dollar mistake. It was the invention of the null reference in 1965 ~Ton e
  • 7. Kotlin - Why? › Statistically typed language for the JVM, Android and the browser. › Highly interoperable with Java › Lightweight (<7000 methods) › Concise - Avoid boilerplate code. › Safe - Convert runtime errors into compile time errors. Null safety guarantees. › Open source community › Supports both, OOP & Functional programming approach. › Very easy to learn
  • 8. Kotlin - Why? Kotlin provides us many additional features (1), as › Null-safety › Extension functions › Type inference for variable and property types › Smart cast › First-class delegation › Lambda expressions + Inline functions = performant custom control structures
  • 9. Kotlin - Why? - additional features (2) › Singletons › Declaration-site variance & Type projections › Range expressions › Companion objects › Data classes › Separate interfaces for read-only and mutable collections
  • 11. Kotlin - Syntax - Data type map
  • 12. Kotlin - Syntax - Properties: val & var ● val is immutable (read-only) and you can only assign a value to them exactly one time. This is the recommended type to use. ● var is mutable and can be reassigned. ● Unit in Kotlin corresponds to the void in Java. Unit is a real class (Singleton) with only one instance. ● Nothing is a type in Kotlin that represents “a value that never exists”, that means just “no value at all”
  • 13. Kotlin - Syntax - String Templates ● Kotlin supports template expressions ● Simple reference uses $ ● Complex references uses ${} ● Raw Strings *See Code examples
  • 14. Kotlin - Syntax - String Templates Kotlin Java
  • 15. Kotlin - Syntax - Null Safety ● No more NPEs ● Possible causes for NPE: ○ !! ○ throw NullPointerException(); ○ External Java code ○ Data inconsistency with regard to initialization
  • 16. Kotlin - Syntax - Visibility Modifiers Modifier › public (default) › internal › protected › private Class member › Visible everywhere › Visible in a module › Visible in subclasses › Visible in a class Top-level declaration › Visible everywhere › Visible in a module › --- › Visible in a file
  • 17. Kotlin - Syntax - Classes ● Many classes in the same file ● Classes and methods are final by default. Use open for extensibility. ● Only properties(public by default) and functions(fun) ● No new keyword on instantiation ● lateinit for properties (not null) and they should be mutable ● init block is equal with the constructor in Java ● Kotlin interfaces are similar to those of Java 8: they can contain definitions of abstract methods as well as implementations of non-abstract methods , but they can’t contain any state.
  • 18. Kotlin - Syntax - Classes ● inner keyword to be able to access members of the surrounding class ○ Nested classes don’t reference their outer class, whereas inner classes do. *See Code examples
  • 19. Kotlin - Syntax - Classes Kotlin Java
  • 20. Kotlin - Syntax - Data Classes | Autogenerated implementations of universal methods Classes that do nothing but hold data. If you add the modifier data to your class, all the necessary methods are automatically generated for you. Compiler will auto generate functions, i.e. copy() function, equals() and hashCode() pair, and toString(). To declare a class data class, you must fulfil these. › The primary constructor must have at least one parameter. › The class cannot be open, abstract, inner or sealed. › The class may extend other classes or implement interfaces.
  • 21. Kotlin - Syntax - Sealed classes To avoid an extra ‘else’ branch, while using ‘when’ expression, Kotlin provided us sealed modifier. Mark a superclass with the sealed modifier, and that restricts the possibility of creating subclasses. All the direct subclasses must be nested in the superclass.
  • 22. Kotlin - Syntax - Object classes Declaring a class and creating an instance, combined, with the object keyword in kotlin now. › object declaration is a way to define a singleton. › companion object can contain factory methods and other methods that are related to this class but don’t require a class instance to be called. Their members can be accessed via class name. › Object expression is used instead of Java’s anonymous inner class. To use a Kotlin object from the Java code, you will access the static INSTANCE field. This will allow to access Kotlin’s objects. *See Code examples
  • 23. Kotlin - Syntax - Object classes
  • 24. Kotlin - Syntax - Operator Overloading & Infix Functions ● Add operators to the classes by using the operator modifier. ● Create infix functions with the infix keyword Infix functions must satisfy the following requirements ○ They must be member functions or extension functions. ○ They must have a single parameter ○ The parameter must not accept variable number of arguments and must have no default value. *See Code examples
  • 25. Kotlin - Syntax - Operator Overloading
  • 26. Kotlin - Syntax - Operator Overloading Expression › a * b › a / b › a % b › a + b › a - b › ++a, a++ › --a, a-- › !a Function name › times › div › mod › plus › minus › inc › Dec › not
  • 27. Kotlin - Syntax - Infix Functions
  • 29.
  • 30. Extension Functions Extension functions are normal static methods bearing no connection with the class they are extending, other than taking an instance of this class as a parameters. We can extend any class with new features even if we don’t have access to the source code The extension function acts as part of the class.Basically, an extension function is a member function of a class that is defined outside the class. › do not modify the original class › the function is added as a static import › can be declared in any file › common practice: create files which include a set of related functions
  • 31. Extension Functions Let’s add a utility function in String class, which will return last character of String. See code below: An extension function ‘lastChar()’ added in String class. In variable ‘output’ we will receive the last character of string, using our extension function.
  • 32. Android KTX Provides Kotlin Extensions for Easier Mobile Development
  • 33. Android-KTX "Kotlin on Android is here to stay, and we have big plans for it." Florina Muntenescu Android developer advocate at Google The team announced the extensions, named Android KTX, for "even sweeter Kotlin development for Android." That sweetness comes by providing more concise code that feels natural to those working in the Android ecosystem. The extensions are provided via APIs atop the base Android framework (available now) and to the Support Library (coming soon). The core-ktx packages on GitHub now show items ranging from androidx.animation to androidx.view.
  • 34. Android-KTX The extensions provide handy autocomplete coding shortcuts for many different operations. While sometimes they just save a few seconds or so by letting coders shorten some small constructs, i.e. It turned the old way of triggering an action with a View's onPreDraw callback from this:
  • 35. Android-KTX Into this: ● Views We have some similar functions available for the View class also. Setting callbacks for layout events is super clean: The postDelayed method is now available as a function:
  • 36. Android-KTX If you need to convert a View instance to a Bitmap, you can do so with this single line of code! Updating the padding for a view is now a lot cleaner and easier to do so.
  • 37. Android-KTX ● ViewGroup There are some handy ViewGroup related functions that you’ll likely be using in your projects! For example, checking a if a viewgroup contains a view: Looping through the children of a viewgroup (where it represents the child):
  • 38. Android-KTX ● Margins Similar to setting the padding for View instances, we can now alter the margins for our layout param instances in a similar manner with the following functions.
  • 39. Android-KTX ● Graphics The Graphics related packages within KTX are pretty beefy. To begin with we now have some handy functions to easily perform operations that convert bitmaps to the following types:
  • 40. Android-KTX KTX offers a collection of operations related to Time, Content Package, OS Package & many new extension functions in Utils. There’s a collection of extensions related to animations within the library and many extensions in Cursor Class. Conclusion is that this is an extensive collection of extension functions, which will make developer’s life much easy. You need to explore and use it!
  • 42. References › https://adtmag.com/articles/2018/02/06/android-ktx.aspx › https://medium.com/exploring-android/exploring-ktx-for- android-13a369795b51 › https://try.kotlinlang.org › https://kotlinlang.org/docs/books.html › https://kotlinlang.org/docs/tutorials/koans.html