SlideShare a Scribd company logo
Kickstart Kotlin
Zahidur Rahman Faisal
Why use Kotlin?
● Simple syntax, easy to read / write
● Powerful features from many other modern languages
● Goodbye NullPointerExceptions
● Completely interoperable with Java
● Great IDE and tooling support
● Code less, do more
● Similarity with Swift, JavaScript or Python
Kickstart Kotlin
Preferences (OSX)
or Settings (Windows/Linux)
> Plugins
> Browse Repositories
> Search “Kotlin”
> Restart Android Studio
Convert your JAVA class
> Select your .java file (Ex: MainActivity.java)
> Code
> Convert Java file to Kotlin file
Goodbye NPE
var nonNullString: String = "Droidcon"
nonNullString = null // Compilation error
val size = nonNullString.length
var nullableString: String? = "Droidcon"
nullableString = null // Compiles fine
val size = nullableString.length // Error:
variable 'nullableString' can be null
val size = nullableString?.length // Null safe
Elvis (Presley?) to the rescue
// Returns length of the String -1 if the String is null
val size = nullableString?.length ?: -1
class Droidcon {
var country: Country?
var host: String?
}
class Country {
var venue: Venue?
var ticketPrice: Int
}
class Venue {
var location: String?
var vanueName: String?
}
JAVA
if (droidCon != null) {
if (droidCon.country != null) {
if (droidCon.country.venue != null) {
... ... ...
println(droidCon.country.venue.location);
}
}
}
No insane sanity checks
Kotlin
print(droidCon?.country?.venue?.location)
POJO vs Data Class
POJO
public class DroidconEvent {
public String topic;
public String speaker;
public long time;
public DroidconEvent(String topic, String speaker, long time) {
this.topic = topic;
this.speaker = speaker;
this.time = time;
}
}
Data Class
data class DroidconEvent(val topic: String,
var speaker: String, val time: Long)
Lazy Lambdas
JAVA
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "Hello Droidcon!",
Toast.LENGTH_SHORT).show();
}
};
Kotlin
mButton.setOnClickListener {
Toast.makeText(this, "Hello Droidcon!", Toast.LENGTH_SHORT).show()
}
Set with “with()”
JAVA
mTextView.setText("Hello Droidcon");
mTextView.setTextSize(30f);
mTextView.setTextColor(Color.WHITE);
mTextView.setBackgroundColor(Color.BLACK);
Kotlin
with (mTextView) {
text = "Hello Droidcon!"
textSize = 30f
setTextColor(Color.WHITE)
setBackgroundColor(Color.BLACK)
}
“When” is the new “Case”
when (x) {
0 -> print("x is 0") // x == 0
in 1..10 -> print("x is in the range") // x inside range 1 - 10
in validNumbers -> print("x is valid") // x is in a collection “validNumbers”
!in 10..20 -> print("x is outside the range") // x is outside range 10 - 20
else -> print("none of the above") // default case
}
Exclusive Extensions
Extension function: (for navigating to a new Activity)
fun Activity.navigateTo(activity: Activity, bundle: Bundle?) {
val intent = Intent(this, activity.javaClass)
bundle ?: intent.putExtra("bundle", bundle)
startActivity(intent)
}
Usage: (from any Activity)
navigateTo(AnotherActivity(), null)
Funny Functions: Default arguments
fun incrementSteps(currentSteps: Int, increment: Int = 1): Int {
return currentSteps + increment
}
Usage:
incrementSteps(5) // Returns 6
incrementSteps(5, 3) // Returns 8
Funny Functions: Named Arguments
fun incrementSteps(currentSteps: Int, increment: Int = 1): Int {
return currentSteps + increment
}
Usage:
incrementSteps(currentSteps = 5, increment = 2) // Returns 7
Funny Functions: Single-Expression Functions
fun doubleSteps(currentSteps: Int) = currentSteps * 2
Usage:
doubleSteps(5) // Returns 10
Funny Functions: Varargs (Variable Arguments)
fun getSum(vararg numbers: Int): Int {
var sum = 0
for (number in numbers) {
sum += number
}
return sum
}
Usage:
getSum(1, 2, 3) // Returns 6
getSum(1, 2, 3, 4, 5) // Returns 15
More Kotlin Awesomeness
https://kotlin.link
http://try.kotlinlang.org
https://github.com/Kotlin/kotlin-koans
https://www.facebook.com/groups/1289612087746759/
Thank You

More Related Content

DOC
Tên hàm
PDF
Introduction of Pharo 5.0
PPTX
Introduction to Node.js
PPTX
Building real-time apps with WebSockets
PPTX
Codable routing
PPTX
Buffer overflow for Beginners
PPTX
Mule esb object_to_json
Tên hàm
Introduction of Pharo 5.0
Introduction to Node.js
Building real-time apps with WebSockets
Codable routing
Buffer overflow for Beginners
Mule esb object_to_json

What's hot (20)

PDF
Node.js for Rubists
PDF
Scaling antispam solutions with Puppet
PPT
20111107 ns2-required cygwinpkg
PPTX
Mule esb object_to_json
PPTX
Muleesbobjecttojson
PDF
Jan Čurn: Meteor: the full-stack JavaScript framework
PPTX
Mule esb json_to_object
PPTX
Node js packages [#howto with npm]
PDF
File input output in Java
KEY
20111126 ns2 installation
PPTX
Mule esb - How to convert from Json to Object in 5 minutes
PDF
Let s Enjoy Node.js
PPTX
How To Deploy And Scale Meteor Applications
PDF
Emscripten, asm.js, and billions of math ops
PDF
Rust All Hands Winter 2011
PDF
Javascript foundations: scope
PDF
Velocity 2011 - Our first DDoS attack
PPT
Ns-2.35 Installation
PDF
Mitmproxy presentation 3:14
PDF
Quick Introduction to Node.js
Node.js for Rubists
Scaling antispam solutions with Puppet
20111107 ns2-required cygwinpkg
Mule esb object_to_json
Muleesbobjecttojson
Jan Čurn: Meteor: the full-stack JavaScript framework
Mule esb json_to_object
Node js packages [#howto with npm]
File input output in Java
20111126 ns2 installation
Mule esb - How to convert from Json to Object in 5 minutes
Let s Enjoy Node.js
How To Deploy And Scale Meteor Applications
Emscripten, asm.js, and billions of math ops
Rust All Hands Winter 2011
Javascript foundations: scope
Velocity 2011 - Our first DDoS attack
Ns-2.35 Installation
Mitmproxy presentation 3:14
Quick Introduction to Node.js
Ad

Similar to Kickstart Kotlin (20)

PPTX
Kotlin – the future of android
PDF
Kotlin for Android - Vali Iorgu - mRready
PDF
Be More Productive with Kotlin
PDF
Kotlin wonderland
PDF
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
PDF
Android 101 - Kotlin ( Future of Android Development)
PDF
Summer of Tech 2017 - Kotlin/Android bootcamp
PDF
Kotlin으로 안드로이드 개발하기
PDF
Coding for Android on steroids with Kotlin
PDF
Kotlin, smarter development for the jvm
PDF
What’s new in Kotlin?
PDF
Coroutines for Kotlin Multiplatform in Practise
PDF
Kotlin for Android Development
PDF
Android 101 - Building a simple app with Kotlin in 90 minutes
PDF
Introduction to TypeScript
PPTX
Kotlin: lo Swift di Android (2015)
PPTX
Kotlin - lo Swift di Android
PDF
Having Fun with Kotlin Android - DILo Surabaya
PDF
Anko - The Ultimate Ninja of Kotlin Libraries?
PDF
2017: Kotlin - now more than ever
Kotlin – the future of android
Kotlin for Android - Vali Iorgu - mRready
Be More Productive with Kotlin
Kotlin wonderland
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Android 101 - Kotlin ( Future of Android Development)
Summer of Tech 2017 - Kotlin/Android bootcamp
Kotlin으로 안드로이드 개발하기
Coding for Android on steroids with Kotlin
Kotlin, smarter development for the jvm
What’s new in Kotlin?
Coroutines for Kotlin Multiplatform in Practise
Kotlin for Android Development
Android 101 - Building a simple app with Kotlin in 90 minutes
Introduction to TypeScript
Kotlin: lo Swift di Android (2015)
Kotlin - lo Swift di Android
Having Fun with Kotlin Android - DILo Surabaya
Anko - The Ultimate Ninja of Kotlin Libraries?
2017: Kotlin - now more than ever
Ad

Recently uploaded (9)

PPTX
ASMS Telecommunication company Profile
PPTX
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
PDF
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
DOC
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
DOC
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
PDF
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
ASMS Telecommunication company Profile
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证

Kickstart Kotlin

  • 2. Why use Kotlin? ● Simple syntax, easy to read / write ● Powerful features from many other modern languages ● Goodbye NullPointerExceptions ● Completely interoperable with Java ● Great IDE and tooling support ● Code less, do more ● Similarity with Swift, JavaScript or Python
  • 3. Kickstart Kotlin Preferences (OSX) or Settings (Windows/Linux) > Plugins > Browse Repositories > Search “Kotlin” > Restart Android Studio
  • 4. Convert your JAVA class > Select your .java file (Ex: MainActivity.java) > Code > Convert Java file to Kotlin file
  • 5. Goodbye NPE var nonNullString: String = "Droidcon" nonNullString = null // Compilation error val size = nonNullString.length var nullableString: String? = "Droidcon" nullableString = null // Compiles fine val size = nullableString.length // Error: variable 'nullableString' can be null val size = nullableString?.length // Null safe
  • 6. Elvis (Presley?) to the rescue // Returns length of the String -1 if the String is null val size = nullableString?.length ?: -1
  • 7. class Droidcon { var country: Country? var host: String? } class Country { var venue: Venue? var ticketPrice: Int } class Venue { var location: String? var vanueName: String? } JAVA if (droidCon != null) { if (droidCon.country != null) { if (droidCon.country.venue != null) { ... ... ... println(droidCon.country.venue.location); } } } No insane sanity checks Kotlin print(droidCon?.country?.venue?.location)
  • 8. POJO vs Data Class POJO public class DroidconEvent { public String topic; public String speaker; public long time; public DroidconEvent(String topic, String speaker, long time) { this.topic = topic; this.speaker = speaker; this.time = time; } } Data Class data class DroidconEvent(val topic: String, var speaker: String, val time: Long)
  • 9. Lazy Lambdas JAVA mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(this, "Hello Droidcon!", Toast.LENGTH_SHORT).show(); } }; Kotlin mButton.setOnClickListener { Toast.makeText(this, "Hello Droidcon!", Toast.LENGTH_SHORT).show() }
  • 10. Set with “with()” JAVA mTextView.setText("Hello Droidcon"); mTextView.setTextSize(30f); mTextView.setTextColor(Color.WHITE); mTextView.setBackgroundColor(Color.BLACK); Kotlin with (mTextView) { text = "Hello Droidcon!" textSize = 30f setTextColor(Color.WHITE) setBackgroundColor(Color.BLACK) }
  • 11. “When” is the new “Case” when (x) { 0 -> print("x is 0") // x == 0 in 1..10 -> print("x is in the range") // x inside range 1 - 10 in validNumbers -> print("x is valid") // x is in a collection “validNumbers” !in 10..20 -> print("x is outside the range") // x is outside range 10 - 20 else -> print("none of the above") // default case }
  • 12. Exclusive Extensions Extension function: (for navigating to a new Activity) fun Activity.navigateTo(activity: Activity, bundle: Bundle?) { val intent = Intent(this, activity.javaClass) bundle ?: intent.putExtra("bundle", bundle) startActivity(intent) } Usage: (from any Activity) navigateTo(AnotherActivity(), null)
  • 13. Funny Functions: Default arguments fun incrementSteps(currentSteps: Int, increment: Int = 1): Int { return currentSteps + increment } Usage: incrementSteps(5) // Returns 6 incrementSteps(5, 3) // Returns 8
  • 14. Funny Functions: Named Arguments fun incrementSteps(currentSteps: Int, increment: Int = 1): Int { return currentSteps + increment } Usage: incrementSteps(currentSteps = 5, increment = 2) // Returns 7
  • 15. Funny Functions: Single-Expression Functions fun doubleSteps(currentSteps: Int) = currentSteps * 2 Usage: doubleSteps(5) // Returns 10
  • 16. Funny Functions: Varargs (Variable Arguments) fun getSum(vararg numbers: Int): Int { var sum = 0 for (number in numbers) { sum += number } return sum } Usage: getSum(1, 2, 3) // Returns 6 getSum(1, 2, 3, 4, 5) // Returns 15