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..
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
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”
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
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
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
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
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.
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!