0% found this document useful (0 votes)
7 views64 pages

DSC MUET Hello To Kotlin

The document introduces Kotlin as a modern programming language, highlighting its official support by Google for Android development and its key features such as null safety, type inference, and interoperability with Java. It compares Kotlin with Java and Swift, emphasizing its elegance and safety, while also providing insights into Kotlin's uptake among developers. Additionally, it lists various resources for learning Kotlin and showcases its syntax through examples.

Uploaded by

Arbaz Pirwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views64 pages

DSC MUET Hello To Kotlin

The document introduces Kotlin as a modern programming language, highlighting its official support by Google for Android development and its key features such as null safety, type inference, and interoperability with Java. It compares Kotlin with Java and Swift, emphasizing its elegance and safety, while also providing insights into Kotlin's uptake among developers. Additionally, it lists various resources for learning Kotlin and showcases its syntax through examples.

Uploaded by

Arbaz Pirwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Hello To Kotlin

@ArbazPirwani
life begins
at the end of your
COMFORT
ZONE
-Neale Donald Walsch-
Kick Start With Kotlin
Arbaz Pirwani
@arbazpirwani
Modern Language
Kotlin Timeline

2016 2017 2018 2019 2020


Kotlin Timeline

1.0

2016 2017 2018 2019 2020


Kotlin Timeline

1.0 Google I/O: Kotlin Officially supported language for Android

2016 2017 2018 2019 2020


Kotlin Timeline

1.0 Google I/O: Android goes Kotlin-first


Google I/O: Kotlin Officially supported language for Android

2016 2017 2018 2019 2020


Kotlin Timeline

1.0 Google I/O: Android goes Kotlin-first


Google I/O: Kotlin Officially supported language for Android Coroutines Preferred

2016 2017 2018 2019 2020


Kotlin Timeline

You are here


1.0 Google I/O: Android goes Kotlin-first
Google I/O: Kotlin Officially supported language for Android Coroutines Preferred

2016 2017 2018 2019 2020


WHY KOTLIN?
- Google Preferred Language
- Statistically typed
- Type Inference
- Developed by Jetbrains
- Concise
- 100% interoperable with Java
- Null Safety
- And many more.
I don’t like Kotlin
I Love Kotlin
Java vs Kotlin
Java to Kotlin
Multiplatform projects

Server
Kotlin/JVM

Android
Kotlin/JVM

iOS Browser
Kotlin/Native Kotlin/JS
Question Of The Day

WHY DO KOTLIN AND JAVA PROGRAMMERS WEAR GLASSES?

BECAUSE THEY DON’T SEE SHARP.


Tools For Kotlin.
https://www.jetbrains.com/idea/download/
Why Kotlin?

Expressiveness Safety

Interoperability Structured
concurrency
Why Kotlin?
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {

...

fab.setOnClickListener { view ->


Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
}

fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {

...

fab.setOnClickListener { view ->


Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
}

fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...

fab.setOnClickListener { view ->


Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
}

fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
}

fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
Template
}
expressions

fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
Template
}
expressions

fab.backgroundTintList = backgroundTintList
}
}
Property access
syntax for getters
and setters
Kotlin Uptake

60% of Pro Android Developers use Kotlin

70% of Top 1000 Apps contain Kotlin code

Source: Google internal data, May 2020


● Data Classes ● Type Inference
● Extension Methods ● DSL Support
● Elvis Operator ● Type-Safe Builders
● Properties ● Coroutines
Default Parameters String Interpolation

Coroutines
● ●
● Type Aliases ● Ranges
● Named Parameters ● Map Array Syntax
● Spread Operator ● Inline Functions
● Auto Casts ● Sealed Classes
● Covariance ● Delegation
● Null Safety ● Destructuring
● Operator Overloading ● KDoc
Coroutines

● Coroutines are like very light-weight threads

● Asynchronous programming made easy

● No callbacks

● No complex functionality
VS
Kotlin & Swift

● The syntax of Swift doesn’t just resemble that of Kotlin: in


small chunks of code there can be up to 77% string
similarity.
● At the very least, it should be noted that while Swift
appeared in 2013, Kotlin originated back in 2011. Hence,
even though comparing Kotlin to Swift (in this exact order)
can be convenient due to Swift’s earlier introduction to a
wide audience, any ‘copycat’ attitudes towards Kotlin
aren’t justified.
Kotlin & Swift

● Available on both mobile-client and back-end server

● Highly performant and statically-typed

● Kotlin also can be used for web front-end via Kotlin.JS


Swift is like Kotlin:
Hello World

Swift
print("Hello, world!")

Kotlin
println("Hello, world!")
Swift is like Kotlin:
Variables And Constants

Swift
var myVariable = 42
myVariable = 50
let myConstant = 42

Kotlin
var myVariable = 42
myVariable = 50
val myConstant = 42
Swift is like Kotlin:
Explicit Types

Swift
let explicitDouble: Double = 42

Kotlin
val explicitDouble: Double = 42.0
Swift is like Kotlin:
Arrays

Swift
var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"

Kotlin
val shoppingList = arrayOf("catfish", "water",
"tulips", "blue paint")
shoppingList[1] = "bottle of water"
Swift is like Kotlin:
Functions

Swift
func greet(_ name: String,_ day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")

Kotlin
fun greet(name: String, day: String): String {
return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Swift is like Kotlin:
Classes
Swift
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}

Kotlin
class Shape {
var numberOfSides = 0
fun simpleDescription()
= "A shape with $numberOfSides sides."
}
Java strikes back with Java 8

• Advantages over Java 7 are overwhelming


• Java 8 introduced
• Lambdas
• Null safety (“Optional” class)
• Default methods
• Streams API
• New Date & Time API
But even with Java 8 Kotlin still...

• Is more elegant, concise and safe


• Has more cool stuff
Basic syntax and rules
More elegant?

• Functions - definition in package or in class


• Immutable/mutable variables
• No “new” keyword
• Type inference
• No checked exceptions
• No primitive types
• No static members
Basic syntax and rules cont’d
More elegant?

• Primary constructors
• No fields, just properties
• Bean style classes easy to declare
• By default, all classes are final
Null Safety
More elegant?

• Null reference – Billion dollar mistake


• Kotlin is designed in a way that aims to eliminate NPE from our code

Tony Hoare
Generics
More elegant?

• Declaration-site variance
• out / in generics modifiers
• Supports use-site variance also
• No wildcard syntax, out / in used
MORE COOL STUFF
Default arguments and
named arguments

• Default argument values can be defined


• Arguments with default values are optional
• No more need for function overloading (almost)
• Kotlin classes can have only one constructor
• Arguments can be called by name
• When passing arguments by name ordering doesn’t matter
Ranges

• Simpler “is it in range” check


• Can be used for any type that implements Comparable
• “both ends“ included
• “..” operator is translated to “rangeTo” function
• “rangeTo” function is implemented as extension function on
Comparable
• Numerical ranges can be iterated over
• In both directions and in arbitrary steps
Ranges
Pattern matching

• Java’s instaceof not very practical


• No !instanceof
• Meet Kotlin’s "is“
• "is" negation "!is“

• Automatic type cast when “is” evaluates true inside if / when


blocks
AND MANY MORE
Resources

• https://kotlinlang.org/
• https://play.kotlinlang.org/
• http://nilhcem.com/swift-is-like-kotlin/
FreeCodeCamp.org
Android Developers
Best Kotlin Learning Resources
Arbaz Pirwani

You might also like