SlideShare a Scribd company logo
1 of 41
Android Knowledge Sharing
Kotlin
Jayaprakash
What’s Kotlin
● Kotlin is a new programming language targeting JVM,
Android and even JavaScript.
● 100% inter-operable with Java™ (Automatic conversion)
● No NPE (NullPointerException)
● Supports Java 8 features like Lambda expressions,
Streams, Try with resource.
● Ability to add methods to Platform APIs.
Why Kotlin is good
● Kotlin compiles to JVM bytecode or JavaScript.
● Kotlin comes from industry, not academia. It solves
problems faced by working programmers today.
● Costs nothing to adopt! It’s open source.
● Kotlin programs can use all existing Java frameworks and
libraries.
● No particular philosophy of programming & runtime
overhead.
● Adopting Kotlin is low risk.
Syntax - Variables
● var - something that will vary with time. (Mutable )
● val - for a value that won’t change. (Immutable - i.e. read-only)
val message : String = "Kotlin Magic" // Explicit definition
var messages: List<String> = ArrayList()
● No new keyword
Syntax - Functions
● Semicolon free language.
● Officially called single-expression functions
● No extra lines and no braces required.
● Lose the “return” keyword and the return type is
automatically inferred.
fun add(a: Int, b: Int): Int {
return a + b
}
fun add(a: Int, b: Int) = a + b
Features - Null-ability
● Catching null(s) during compile time.
● While declaring a variable as null:
● var message: String = null // Compile-time error
● var message: String? = null // Okay
● Compiler shows compile time error if an object is null-
able
message?.let { println(it) }
Features - Elvis operator ?:
If something is null, I want to give it a value, but
otherwise just leave it alone.
// Java
if (people == null) {
people = new ArrayList();
}
return people;
// Kotlin
return people ?: emptyArrayList()
Features - Mutability
● Compiler comes with Mutability Protection to prevent NPEs
on var type variables.
● Thread-safe & no synchronization issues
● Concurrent programming safer and cleaner.
Mutable object – Can change the states
and fields after the object is created. Ex:
StringBuilder
Immutable object – Cannot change
anything after the object is created. Ex:
String
Features - Lambda Expressions
● Higher Order Function - passed as an argument to another
function.
● Beautifying even the ugliest click handlers,
mButton.setOnClickListener {
// Your click logic
}
● Cut to the main logic & code so clean and simple.
Feature - Smart Casting
if (view is TextView) {
view.setText("So much redundancy"); // Smart Cast
}
● The Compiler does smart casting on our behalf.
● Handles the negative scenario like if the “view” is not
an “TextView”.
Setting up Kotlin in Android Studio
● Open Android Studio Settings (Ctrl + Alt + S)
● Go to Plugins
● Install only the “Kotlin” plugin
● Restart the IDE
Tools > Kotlin > Configure Kotlin in Project
Convert Java to Kotlin file - Just Copy & paste
Cool things in Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
● Inheritance- No “extends” & “implements”,Replaced with a
“:”
● “?” - Allow you to use the object in case it value exists
otherwise it will ignore it.
● val resource = context?.getResources() // Safe
If context has value,
// A better approach
if (context != null) {
// Smart cast, Thank you Compiler!
val res = context.getResources()
}
// Another approach
context?.let {
// Execute this block if not null
val res = context.getResources() }
// Yet another approach
context?.apply {
// Execute this block if not null
val res = getResources() }
Let & Apply block
Want to define a variable for a
specific scope of your code but
not beyond.
Similar to let block
Used to test null condition Part of the calling type
Useful to keep your code nicely
self-contained so that you don’t
have variables “leaking out”.
i.e. not calling
context.getResources() but
instead we are directly calling
getResources()
Comes with standard library of Kotlin
Let Apply
apply vs with
‘apply’ accepts an instance as the receiver while ‘with’ requires an instance
to be passed as an argument.
fun getDeveloper(): Developer {
return Developer().apply {
developerName = "Amit Shekhar"
developerAge = 22
}
}
Usually you use apply when you need to do something with an object and return it. And when
you need to perform some operations on an object and return some other object you can use
with.
fun getPersonFromDeveloper(developer: Developer): Person {
return with(developer) {
Person(developerName, developerAge)
}
}
Free Functions
● Functions or variables can be declared at the top level
in a file (*.kt) without any class.
● Default static in nature without any class holding them.
● Can write free functions and access them from another
Kotlin class or file directly (without Class name).
● But, functions come with access specifiers to control the
visibility.
Goodbyes to findViewById
● Kotlin (specifically Android Extensions) do the heavy
lifting for you.
apply plugin: 'kotlin-android-extensions' in gradle
● For the layout filename activity_main.xml, we'd need to
import
import kotlinx.android.synthetic.main.activity_main.*
● txtTitle.text = "Hello Kotlin"
findViewById - At Background,
● No more view binding.
● Internally the compiler creates a small hidden cache
● Activity or Fragment giving us the synthetic property to
use the view.
● Lazy initialized i.e. when you use the view in the code
● Next time you use the same view it will fetch it from
cache.
findViewById - Exceptions
● Android Extension doesn’t work on MenuItem.
● Could be referencing to a layout that is not a direct
children of that one.
● Compiler won’t give you warning or error in this case But
it will fail during the execution time.
● When it tries to get the view that doesn’t exist.
lateinit vs lazy (Property initialization feature)
lateinit ( late initialization)
● Properties can be initialized through dependency
injection, or in the setup method of a unit test.
public class Test {
lateinit var mock: Mock
@SetUp fun setup() {
mock = Mock()
}
@Test fun test() {
mock.do()
}
}
lateinit ..,
● Cannot supply a non-null initializer in the constructor,
but you still want to avoid null checks.
● To handle this case, you can mark the property with the
lateinit modifier.
● Only be used on var properties declared inside the body
of a class.
● And when the property does not have a custom getter or
setter.
lazy function ( lazy initialization)
● Takes a lambda and returns an instance of lazy.
● Serve as a delegate for implementing a lazy property.
● First call to get() executes the lambda passed to lazy()
and remembers the result.
public class Example{
val name: String by lazy { “Steve jobs” }
}
First and subsequent calls, name will return “Steve jobs”
When to use which one?
● Lazy is a good fit for properties that may or may not be
accessed.
● If we never access them, we avoid computing their initial
value.
● Good for Activity - Not accessed before setContentView
● Crash in Fragment - View configuration is done in
onViewCreated
When to use which one?
● Activities or Fragments - Makes more sense to use
lateinit for their properties, especially the ones
referencing views.
● Don’t control the lifecycle, we know when those
properties will be properly initialized.
● Downside - Ensure they are initialized in the
appropriate lifecycle methods.
Data Class
data class Developer(val name: String, val age: Int)
● No need hashCode(), equals(), toString(), and copy()
● Compiler automatically create these internally, so it also leads to clean
code.
Requirements that data classes need to fulfil:
● The primary constructor needs to have at least one parameter.
● All primary constructor parameters need to be marked as val or var
● Data classes cannot be abstract, open, sealed or inner.
Destructuring - Returning Two Values from a
Function
Convenient way of extracting multiple values from data.
data class Developer(val name: String, val age: Int)
fun getDeveloper(): Developer {
// some logic
return Developer(name, age)
}
// Now, to use this function:
val (name, age) = getDeveloper()
Destructuring Declarations and Maps
for ((key, value) in map) {
// do something with the key and the value
}
sealed classes
● Restricted class hierarchies, also allowing a datatype to be one of a
predefined set of types.
● Combined with when expression & Verify all cases and else unnecessary.
sealed class Car {
data class Maruti(val speed: Int) : Car()
data class Bugatti(val speed: Int, val boost: Int) : Car()
object NotACar : Car()
}
fun speed(car: Car): Int = when (car) {
is Car.Maruti -> car.speed
is Car.Bugatti -> car.speed + car.boost
Car.NotACar -> INVALID_SPEED
// else clause is not required as we've covered all the cases
}
Sealed classes have the same behavior as enum
classes
● An extension of enum classes: the set of values for an
enum type is also restricted
● enum - single instance, a subclasses of a sealed class -
multiple instances.
● Key benefit of using sealed classes comes into play when
you use them in a when expression.
● If constant behavior, so your pick is an enum, otherwise,
sealed is your choice.
Replacing simple if/else if/else/switch blocks with
when
if (firstName.equals("Dan")) {
person.setTeam(programmers);
} else if (lastName.equals("Dihiansan")) {
person.setTeam(designers);
} else {
person.setTeam(others);
}
switch (firstName) {
case "Dan":
person.setTeam(programmers)
break;
case "Jay":
person.setTeam(programmers)
break;
case "Jamie":
person.setTeam(designers)
break;
default:
person.setTeam(others)
}
In JAVA
Replacing simple if/else if/else/switch blocks with
when
In Kotlin
when {
firstName == "Dan" -> person.team = programmers
lastName == "Dihiansan" -> person.team = designers
else -> person.team = others
}
when (firstName) {
"Dan", "Jay" -> person.team = programmers
"Jamie" -> person.team = designers
else -> person.team = others
}
Convenience methods built on top of familiar
objects
Extended objects you’re familiar with and made them even
better and packaged them into the Kotlin Standard Library.
// Java
if (name.toLowerCase().contains(firstName.toLowerCase())) {
...
}
// Kotlin
if (name.contains(firstName, true)) { ... }
Best Practices - do things the Kotlin way
1. Named Arguments instead of Fluent Setter
2. apply() for Grouping Object Initialization
3. Don’t Overload for Default Arguments
4. Leverage Value Objects
5. Refer to Constructor Parameters in Property Initializers
6. Ad-Hoc Creation of Structs
1. Named Arguments instead of Fluent Setter
● Simulate named and default arguments and to make huge
parameter lists
● In Kotlin, named and default arguments fulfil the same
propose but are built directly into the language
//Don't
val config = SearchConfig()
.setRoot("~/folder")
.setTerm("kotlin")
.setRecursive(true)
.setFollowSymlinks(true)
//Do
val config2 = SearchConfig2(
root = "~/folder",
term = "kotlin",
recursive = true,
followSymlinks = true
)
2. apply() for Grouping Object Initialization
● Helps to group and centralize initialization code for an
object.
● apply() is often useful when dealing with Java libraries
in Kotlin.
//Don't
val dataSource = BasicDataSource()
dataSource.driverClassName =
"com.mysql.jdbc.Driver"
dataSource.url =
"jdbc:mysql://domain:3309/db"
dataSource.username = "username"
dataSource.password = "password"
//Do
val dataSource = BasicDataSource().apply {
driverClassName =
"com.mysql.jdbc.Driver"
url = "jdbc:mysql://domain:3309/db"
username = "username"
password = "password" }
3. Don’t Overload for Default Arguments
● Don’t overload methods and constructors to realize
default arguments (so called “method chaining” or
“constructor chaining”).
● In fact, default arguments remove nearly all use cases
for method and constructor overloading
//Don't
fun find(name: String){
find(name, true)
}
fun find(name: String, recursive: Boolean){
}
//Do
fun find(name: String, recursive: Boolean = true){
}
4. Leverage Value Objects
● With data classes, writing immutable value objects is so
easy.
● Even for value objects containing only a single property.
● So there is no excuse for not using value objects
anymore!
//Don't
fun send(target: String){}
//Do
fun send(target: EmailAddress){}
// expressive, readable, type-safe
data class EmailAddress(val value: String)
5. Refer to Constructor Parameters in Property
Initializers
● Define a constructor body (init block) only to initialize
properties.
● apply() can help to group initialization code and get
along with a single expression.
//Don't
class UsersClient(baseUrl: String, appName: String) {
private val httpClient: HttpClient
init {
val builder = HttpClientBuilder.create()
builder.setUserAgent(appName)
builder.setConnectionTimeToLive(10,
TimeUnit.SECONDS)
httpClient = builder.build()
}
}
//Do
class UsersClient(baseUrl: String, appName:
String) {
private val httpClient =
HttpClientBuilder.create().apply {
setUserAgent(appName)
setConnectionTimeToLive(10,
TimeUnit.SECONDS)
}.build()
}
6. Ad-Hoc Creation of Structs
● listOf, mapOf and the infix function to can be used to
create structs (like JSON) quite concisely.
● Use data classes and object mapping to create JSON. But
sometimes (e.g. in tests) this is very useful.
//Do
val customer = mapOf(
"name" to "Clair Grube",
"languages" to listOf("german", "english"),
"address" to mapOf(
"city" to "Leipzig",
)
)
References
https://kotlinlang.org/docs/tutorials/kotlin-android.html
https://kotlinlang.org/docs/reference/
https://blog.mindorks.com/a-complete-guide-to-learn-kotlin-for-android-development-
b1e5d23cc2d8
https://blog.philipphauer.de/idiomatic-kotlin-best-practices/
Kotlin interactive programming
https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Hello,%20world!/Task.kt
Kotlin for Android Knowledge Sharing
Kotlin for Android Knowledge Sharing

More Related Content

What's hot

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptRangana Sampath
 
Tips and Tricks for Testing Lambda Expressions in Android
Tips and Tricks for Testing Lambda Expressions in AndroidTips and Tricks for Testing Lambda Expressions in Android
Tips and Tricks for Testing Lambda Expressions in AndroidDavid Carver
 
Kotlin a problem solver - gdd extended pune
Kotlin   a problem solver - gdd extended puneKotlin   a problem solver - gdd extended pune
Kotlin a problem solver - gdd extended puneHardik Trivedi
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageAzilen Technologies Pvt. Ltd.
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and ClassesMichael Heron
 
ParaSail
ParaSail  ParaSail
ParaSail AdaCore
 
Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developertroubledkumi
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_scriptVijay Kalyan
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noiseNeeraj Bhusare
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
Functional programing jargon
Functional programing jargonFunctional programing jargon
Functional programing jargonRemo Jansen
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaKnoldus Inc.
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Java Serialization
Java SerializationJava Serialization
Java Serializationjeslie
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 

What's hot (20)

Xtext Webinar
Xtext WebinarXtext Webinar
Xtext Webinar
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Tips and Tricks for Testing Lambda Expressions in Android
Tips and Tricks for Testing Lambda Expressions in AndroidTips and Tricks for Testing Lambda Expressions in Android
Tips and Tricks for Testing Lambda Expressions in Android
 
Kotlin a problem solver - gdd extended pune
Kotlin   a problem solver - gdd extended puneKotlin   a problem solver - gdd extended pune
Kotlin a problem solver - gdd extended pune
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
ParaSail
ParaSail  ParaSail
ParaSail
 
Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developer
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Functional programing jargon
Functional programing jargonFunctional programing jargon
Functional programing jargon
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Functional Go
Functional GoFunctional Go
Functional Go
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 

Similar to Kotlin for Android Knowledge Sharing

Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyMobileAcademy
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsRavi Bhadauria
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksStoyan Nikolov
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objectsSandeep Chawla
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageHossam Ghareeb
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developersMohamed Wael
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2Chris Farrell
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?Squareboat
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sampleprahalad_das_in
 
C questions
C questionsC questions
C questionsparm112
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Arnaud Giuliani
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James NelsonGWTcon
 

Similar to Kotlin for Android Knowledge Sharing (20)

Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Frontend training
Frontend trainingFrontend training
Frontend training
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sample
 
Java Enterprise Edition
Java Enterprise EditionJava Enterprise Edition
Java Enterprise Edition
 
C questions
C questionsC questions
C questions
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
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
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
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...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
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
 

Kotlin for Android Knowledge Sharing

  • 2. What’s Kotlin ● Kotlin is a new programming language targeting JVM, Android and even JavaScript. ● 100% inter-operable with Java™ (Automatic conversion) ● No NPE (NullPointerException) ● Supports Java 8 features like Lambda expressions, Streams, Try with resource. ● Ability to add methods to Platform APIs.
  • 3. Why Kotlin is good ● Kotlin compiles to JVM bytecode or JavaScript. ● Kotlin comes from industry, not academia. It solves problems faced by working programmers today. ● Costs nothing to adopt! It’s open source. ● Kotlin programs can use all existing Java frameworks and libraries. ● No particular philosophy of programming & runtime overhead. ● Adopting Kotlin is low risk.
  • 4. Syntax - Variables ● var - something that will vary with time. (Mutable ) ● val - for a value that won’t change. (Immutable - i.e. read-only) val message : String = "Kotlin Magic" // Explicit definition var messages: List<String> = ArrayList() ● No new keyword
  • 5. Syntax - Functions ● Semicolon free language. ● Officially called single-expression functions ● No extra lines and no braces required. ● Lose the “return” keyword and the return type is automatically inferred. fun add(a: Int, b: Int): Int { return a + b } fun add(a: Int, b: Int) = a + b
  • 6. Features - Null-ability ● Catching null(s) during compile time. ● While declaring a variable as null: ● var message: String = null // Compile-time error ● var message: String? = null // Okay ● Compiler shows compile time error if an object is null- able message?.let { println(it) }
  • 7. Features - Elvis operator ?: If something is null, I want to give it a value, but otherwise just leave it alone. // Java if (people == null) { people = new ArrayList(); } return people; // Kotlin return people ?: emptyArrayList()
  • 8. Features - Mutability ● Compiler comes with Mutability Protection to prevent NPEs on var type variables. ● Thread-safe & no synchronization issues ● Concurrent programming safer and cleaner. Mutable object – Can change the states and fields after the object is created. Ex: StringBuilder Immutable object – Cannot change anything after the object is created. Ex: String
  • 9. Features - Lambda Expressions ● Higher Order Function - passed as an argument to another function. ● Beautifying even the ugliest click handlers, mButton.setOnClickListener { // Your click logic } ● Cut to the main logic & code so clean and simple.
  • 10. Feature - Smart Casting if (view is TextView) { view.setText("So much redundancy"); // Smart Cast } ● The Compiler does smart casting on our behalf. ● Handles the negative scenario like if the “view” is not an “TextView”.
  • 11. Setting up Kotlin in Android Studio ● Open Android Studio Settings (Ctrl + Alt + S) ● Go to Plugins ● Install only the “Kotlin” plugin ● Restart the IDE Tools > Kotlin > Configure Kotlin in Project Convert Java to Kotlin file - Just Copy & paste
  • 12. Cool things in Kotlin class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } ● Inheritance- No “extends” & “implements”,Replaced with a “:” ● “?” - Allow you to use the object in case it value exists otherwise it will ignore it. ● val resource = context?.getResources() // Safe
  • 13. If context has value, // A better approach if (context != null) { // Smart cast, Thank you Compiler! val res = context.getResources() } // Another approach context?.let { // Execute this block if not null val res = context.getResources() } // Yet another approach context?.apply { // Execute this block if not null val res = getResources() }
  • 14. Let & Apply block Want to define a variable for a specific scope of your code but not beyond. Similar to let block Used to test null condition Part of the calling type Useful to keep your code nicely self-contained so that you don’t have variables “leaking out”. i.e. not calling context.getResources() but instead we are directly calling getResources() Comes with standard library of Kotlin Let Apply
  • 15. apply vs with ‘apply’ accepts an instance as the receiver while ‘with’ requires an instance to be passed as an argument. fun getDeveloper(): Developer { return Developer().apply { developerName = "Amit Shekhar" developerAge = 22 } } Usually you use apply when you need to do something with an object and return it. And when you need to perform some operations on an object and return some other object you can use with. fun getPersonFromDeveloper(developer: Developer): Person { return with(developer) { Person(developerName, developerAge) } }
  • 16. Free Functions ● Functions or variables can be declared at the top level in a file (*.kt) without any class. ● Default static in nature without any class holding them. ● Can write free functions and access them from another Kotlin class or file directly (without Class name). ● But, functions come with access specifiers to control the visibility.
  • 17. Goodbyes to findViewById ● Kotlin (specifically Android Extensions) do the heavy lifting for you. apply plugin: 'kotlin-android-extensions' in gradle ● For the layout filename activity_main.xml, we'd need to import import kotlinx.android.synthetic.main.activity_main.* ● txtTitle.text = "Hello Kotlin"
  • 18. findViewById - At Background, ● No more view binding. ● Internally the compiler creates a small hidden cache ● Activity or Fragment giving us the synthetic property to use the view. ● Lazy initialized i.e. when you use the view in the code ● Next time you use the same view it will fetch it from cache.
  • 19. findViewById - Exceptions ● Android Extension doesn’t work on MenuItem. ● Could be referencing to a layout that is not a direct children of that one. ● Compiler won’t give you warning or error in this case But it will fail during the execution time. ● When it tries to get the view that doesn’t exist.
  • 20. lateinit vs lazy (Property initialization feature) lateinit ( late initialization) ● Properties can be initialized through dependency injection, or in the setup method of a unit test. public class Test { lateinit var mock: Mock @SetUp fun setup() { mock = Mock() } @Test fun test() { mock.do() } }
  • 21. lateinit .., ● Cannot supply a non-null initializer in the constructor, but you still want to avoid null checks. ● To handle this case, you can mark the property with the lateinit modifier. ● Only be used on var properties declared inside the body of a class. ● And when the property does not have a custom getter or setter.
  • 22. lazy function ( lazy initialization) ● Takes a lambda and returns an instance of lazy. ● Serve as a delegate for implementing a lazy property. ● First call to get() executes the lambda passed to lazy() and remembers the result. public class Example{ val name: String by lazy { “Steve jobs” } } First and subsequent calls, name will return “Steve jobs”
  • 23. When to use which one? ● Lazy is a good fit for properties that may or may not be accessed. ● If we never access them, we avoid computing their initial value. ● Good for Activity - Not accessed before setContentView ● Crash in Fragment - View configuration is done in onViewCreated
  • 24. When to use which one? ● Activities or Fragments - Makes more sense to use lateinit for their properties, especially the ones referencing views. ● Don’t control the lifecycle, we know when those properties will be properly initialized. ● Downside - Ensure they are initialized in the appropriate lifecycle methods.
  • 25. Data Class data class Developer(val name: String, val age: Int) ● No need hashCode(), equals(), toString(), and copy() ● Compiler automatically create these internally, so it also leads to clean code. Requirements that data classes need to fulfil: ● The primary constructor needs to have at least one parameter. ● All primary constructor parameters need to be marked as val or var ● Data classes cannot be abstract, open, sealed or inner.
  • 26. Destructuring - Returning Two Values from a Function Convenient way of extracting multiple values from data. data class Developer(val name: String, val age: Int) fun getDeveloper(): Developer { // some logic return Developer(name, age) } // Now, to use this function: val (name, age) = getDeveloper() Destructuring Declarations and Maps for ((key, value) in map) { // do something with the key and the value }
  • 27. sealed classes ● Restricted class hierarchies, also allowing a datatype to be one of a predefined set of types. ● Combined with when expression & Verify all cases and else unnecessary. sealed class Car { data class Maruti(val speed: Int) : Car() data class Bugatti(val speed: Int, val boost: Int) : Car() object NotACar : Car() } fun speed(car: Car): Int = when (car) { is Car.Maruti -> car.speed is Car.Bugatti -> car.speed + car.boost Car.NotACar -> INVALID_SPEED // else clause is not required as we've covered all the cases }
  • 28. Sealed classes have the same behavior as enum classes ● An extension of enum classes: the set of values for an enum type is also restricted ● enum - single instance, a subclasses of a sealed class - multiple instances. ● Key benefit of using sealed classes comes into play when you use them in a when expression. ● If constant behavior, so your pick is an enum, otherwise, sealed is your choice.
  • 29. Replacing simple if/else if/else/switch blocks with when if (firstName.equals("Dan")) { person.setTeam(programmers); } else if (lastName.equals("Dihiansan")) { person.setTeam(designers); } else { person.setTeam(others); } switch (firstName) { case "Dan": person.setTeam(programmers) break; case "Jay": person.setTeam(programmers) break; case "Jamie": person.setTeam(designers) break; default: person.setTeam(others) } In JAVA
  • 30. Replacing simple if/else if/else/switch blocks with when In Kotlin when { firstName == "Dan" -> person.team = programmers lastName == "Dihiansan" -> person.team = designers else -> person.team = others } when (firstName) { "Dan", "Jay" -> person.team = programmers "Jamie" -> person.team = designers else -> person.team = others }
  • 31. Convenience methods built on top of familiar objects Extended objects you’re familiar with and made them even better and packaged them into the Kotlin Standard Library. // Java if (name.toLowerCase().contains(firstName.toLowerCase())) { ... } // Kotlin if (name.contains(firstName, true)) { ... }
  • 32. Best Practices - do things the Kotlin way 1. Named Arguments instead of Fluent Setter 2. apply() for Grouping Object Initialization 3. Don’t Overload for Default Arguments 4. Leverage Value Objects 5. Refer to Constructor Parameters in Property Initializers 6. Ad-Hoc Creation of Structs
  • 33. 1. Named Arguments instead of Fluent Setter ● Simulate named and default arguments and to make huge parameter lists ● In Kotlin, named and default arguments fulfil the same propose but are built directly into the language //Don't val config = SearchConfig() .setRoot("~/folder") .setTerm("kotlin") .setRecursive(true) .setFollowSymlinks(true) //Do val config2 = SearchConfig2( root = "~/folder", term = "kotlin", recursive = true, followSymlinks = true )
  • 34. 2. apply() for Grouping Object Initialization ● Helps to group and centralize initialization code for an object. ● apply() is often useful when dealing with Java libraries in Kotlin. //Don't val dataSource = BasicDataSource() dataSource.driverClassName = "com.mysql.jdbc.Driver" dataSource.url = "jdbc:mysql://domain:3309/db" dataSource.username = "username" dataSource.password = "password" //Do val dataSource = BasicDataSource().apply { driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://domain:3309/db" username = "username" password = "password" }
  • 35. 3. Don’t Overload for Default Arguments ● Don’t overload methods and constructors to realize default arguments (so called “method chaining” or “constructor chaining”). ● In fact, default arguments remove nearly all use cases for method and constructor overloading //Don't fun find(name: String){ find(name, true) } fun find(name: String, recursive: Boolean){ } //Do fun find(name: String, recursive: Boolean = true){ }
  • 36. 4. Leverage Value Objects ● With data classes, writing immutable value objects is so easy. ● Even for value objects containing only a single property. ● So there is no excuse for not using value objects anymore! //Don't fun send(target: String){} //Do fun send(target: EmailAddress){} // expressive, readable, type-safe data class EmailAddress(val value: String)
  • 37. 5. Refer to Constructor Parameters in Property Initializers ● Define a constructor body (init block) only to initialize properties. ● apply() can help to group initialization code and get along with a single expression. //Don't class UsersClient(baseUrl: String, appName: String) { private val httpClient: HttpClient init { val builder = HttpClientBuilder.create() builder.setUserAgent(appName) builder.setConnectionTimeToLive(10, TimeUnit.SECONDS) httpClient = builder.build() } } //Do class UsersClient(baseUrl: String, appName: String) { private val httpClient = HttpClientBuilder.create().apply { setUserAgent(appName) setConnectionTimeToLive(10, TimeUnit.SECONDS) }.build() }
  • 38. 6. Ad-Hoc Creation of Structs ● listOf, mapOf and the infix function to can be used to create structs (like JSON) quite concisely. ● Use data classes and object mapping to create JSON. But sometimes (e.g. in tests) this is very useful. //Do val customer = mapOf( "name" to "Clair Grube", "languages" to listOf("german", "english"), "address" to mapOf( "city" to "Leipzig", ) )