SlideShare a Scribd company logo
Kotlin for Android
May 24, 2018
y/kotlin-workbook
Top Level Declarations
Classes, Interfaces, Functions and Properties
y/kotlin-workbook
● Everything is a type - there are no primitives
Types
BASIC SYNTAX
y/kotlin-workbook
Kotlin Java
Boolean boolean, Boolean
Int int, Integer
Same for char, byte, short, long, float and double
Any Object
Unit void
Nothing void
Array<Int> Integer[]
IntArray int[]
Same for BooleanArray, CharArray, ByteArray, ...
package & import
BASIC SYNTAX
package my.demo
import android.view.View
import android.view.View.VISIBLE
import my.demo.Contract.View as CView
y/kotlin-workbook
● val and var
● Strong compiler type inference
Variables
BASIC SYNTAX
y/kotlin-workbook
Variables
BASIC SYNTAX
// Java
final int a = 1;
int b = 2;
// Kotlin
val a: Int = 1
var b: Int = 2
y/kotlin-workbook
Variables
BASIC SYNTAX
// Java
final int a = 1;
int b = 2;
// Kotlin
val a = 1
var b = 2
y/kotlin-workbook
const
BASIC SYNTAX
y/kotlin-workbook
● Only for val
● Stricter than Java’s static
● Available only at top level and inside an object
const
BASIC SYNTAX
const val ONE = 1
const val FOO = "FOO"
const val FOO1 = "FOO" + ONE
y/kotlin-workbook
● Only for val
● Stricter than Java’s static
● Available only at top level and inside an object
● Native suport for multiline strings
● All Strings literals are templates
● Templates are evaluated at compile-time
● Faster than String.format()
Strings
BASIC SYNTAX
y/kotlin-workbook
const val W = "World"
"Hello $W!"
val map = mutableMapOf("foo" to "bar")
"foo is ${map["foo"]}"
Strings
BASIC SYNTAX
y/kotlin-workbook
val w = "World"
println("Hello $w!") // prints "Hello World!"
val map = mutableMapOf("foo" to "bar")
println("foo is ${map["foo"]}") // prints "foo is bar"
map["bar"] = "baz"
println("""
foo is ${map["foo"]}
bar is ${map["bar"]}
""")
Strings
BASIC SYNTAX
y/kotlin-workbook
println("$1 $bills") // "$1 $bills"
println("""$1 ${'$'}bills""") // "$1 $bills"
Strings
BASIC SYNTAX
y/kotlin-workbook
● Can be declared at top level
● Can be declared inside other functions
● Can take other functions as parameters
● Can return functions
functions
BASIC SYNTAX
y/kotlin-workbook
fun world(): String {
return "World"
}
functions
BASIC SYNTAX
y/kotlin-workbook
fun world(): String {
return "World"
}
fun world(): String = "World"
functions
BASIC SYNTAX
y/kotlin-workbook
fun world(): String {
return "World"
}
fun world(): String = "World"
fun world() = "World"
fun hello(who: String) = "Hello $who"
functions
BASIC SYNTAX
y/kotlin-workbook
● A single class, interface or object per file
● final is default modality
○ open and override keyword to declare extensibility
● public is default visibility
○ internal visibility instead of java default
● No new keyword
class & interface
BASIC SYNTAX
y/kotlin-workbook
interfaces
BASIC SYNTAX
interface Runnable {
fun run()
}
y/kotlin-workbook
interfaces
BASIC SYNTAX
interface Runnable {
fun run()
}
interface RepeatingRunnable : Runnable {
fun run(times: Int) {
(1..times).forEach { run() }
}
}
y/kotlin-workbook
classes
BASIC SYNTAX
y/kotlin-workbook
interface Runnable {
fun run()
}
interface RepeatingRunnable : Runnable {
fun run(times: Int) {
(1..times).forEach { run() }
}
}
open class Greeting : Runnable {
override fun run() = println("Hi!")
}
classes
BASIC SYNTAX
y/kotlin-workbook
interface Runnable {
fun run()
}
interface RepeatingRunnable : Runnable {
fun run(times: Int) {
(1..times).forEach { run() }
}
}
open class Greeting : Runnable {
override fun run() = println("Hi!")
}
class RepeatingGreeting : Greeting(), RepeatingRunnable
RepeatingGreeting().run(3)
Questions?
Next Up: Constructors and Control Flow

More Related Content

What's hot (20)

PPTX
Kotlin on android
Kurt Renzo Acosta
 
PPT
Functions in C++
Sachin Sharma
 
PPT
Lecture05
elearning_portal
 
PDF
03 function overloading
Jasleen Kaur (Chandigarh University)
 
PPT
3 Function Overloading
Praveen M Jigajinni
 
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
PPTX
Presentation on overloading
Charndeep Sekhon
 
PPTX
Javascript ch6
Brady Cheng
 
PDF
GraphQL API in Clojure
Kent Ohashi
 
PPT
Function overloading(C++)
Ritika Sharma
 
PDF
Coffee script
Futada Takashi
 
PPTX
Operator overloading
ramya marichamy
 
PPTX
Mca 2nd sem u-4 operator overloading
Rai University
 
PPTX
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
PPTX
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
PPTX
Inline function
Tech_MX
 
PDF
Halogen: Past, Present, and Future
John De Goes
 
PPTX
Kotlin
Rory Preddy
 
PPT
Operator overloading
abhay singh
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
Kotlin on android
Kurt Renzo Acosta
 
Functions in C++
Sachin Sharma
 
Lecture05
elearning_portal
 
03 function overloading
Jasleen Kaur (Chandigarh University)
 
3 Function Overloading
Praveen M Jigajinni
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Presentation on overloading
Charndeep Sekhon
 
Javascript ch6
Brady Cheng
 
GraphQL API in Clojure
Kent Ohashi
 
Function overloading(C++)
Ritika Sharma
 
Coffee script
Futada Takashi
 
Operator overloading
ramya marichamy
 
Mca 2nd sem u-4 operator overloading
Rai University
 
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
Inline function
Tech_MX
 
Halogen: Past, Present, and Future
John De Goes
 
Kotlin
Rory Preddy
 
Operator overloading
abhay singh
 
FUNCTIONS IN c++ PPT
03062679929
 

Similar to Kotlin For Android - Basics (part 1 of 7) (20)

PPTX
Kotlin For Android - Functions (part 3 of 7)
Gesh Markov
 
PPTX
Introduction to Kotlin for Android developers
Mohamed Wael
 
PDF
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
PDF
Exploring Kotlin
Johan Haleby
 
PPTX
Kotlin Basic & Android Programming
Kongu Engineering College, Perundurai, Erode
 
PDF
Coding for Android on steroids with Kotlin
Kai Koenig
 
PDF
Android 101 - Building a simple app with Kotlin in 90 minutes
Kai Koenig
 
PPTX
Kotlin compiler construction (very brief)
Ildar Nurgaliev
 
PPTX
kotlin-nutshell.pptx
AbdulRazaqAnjum
 
PDF
Intro to Kotlin
Magda Miu
 
PDF
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
STX Next
 
PDF
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
PDF
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
PDF
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
PDF
Kotlin for Android devs
Adit Lal
 
PDF
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig
 
PDF
Little Helpers for Android Development with Kotlin
Kai Koenig
 
PDF
Privet Kotlin (Windy City DevFest)
Cody Engel
 
PDF
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 
PPTX
Kotlin presentation
MobileAcademy
 
Kotlin For Android - Functions (part 3 of 7)
Gesh Markov
 
Introduction to Kotlin for Android developers
Mohamed Wael
 
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
Exploring Kotlin
Johan Haleby
 
Kotlin Basic & Android Programming
Kongu Engineering College, Perundurai, Erode
 
Coding for Android on steroids with Kotlin
Kai Koenig
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Kai Koenig
 
Kotlin compiler construction (very brief)
Ildar Nurgaliev
 
kotlin-nutshell.pptx
AbdulRazaqAnjum
 
Intro to Kotlin
Magda Miu
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
STX Next
 
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
Kotlin for Android devs
Adit Lal
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig
 
Little Helpers for Android Development with Kotlin
Kai Koenig
 
Privet Kotlin (Windy City DevFest)
Cody Engel
 
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 
Kotlin presentation
MobileAcademy
 
Ad

Recently uploaded (20)

PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

Kotlin For Android - Basics (part 1 of 7)

  • 1. Kotlin for Android May 24, 2018 y/kotlin-workbook
  • 2. Top Level Declarations Classes, Interfaces, Functions and Properties y/kotlin-workbook
  • 3. ● Everything is a type - there are no primitives Types BASIC SYNTAX y/kotlin-workbook Kotlin Java Boolean boolean, Boolean Int int, Integer Same for char, byte, short, long, float and double Any Object Unit void Nothing void Array<Int> Integer[] IntArray int[] Same for BooleanArray, CharArray, ByteArray, ...
  • 4. package & import BASIC SYNTAX package my.demo import android.view.View import android.view.View.VISIBLE import my.demo.Contract.View as CView y/kotlin-workbook
  • 5. ● val and var ● Strong compiler type inference Variables BASIC SYNTAX y/kotlin-workbook
  • 6. Variables BASIC SYNTAX // Java final int a = 1; int b = 2; // Kotlin val a: Int = 1 var b: Int = 2 y/kotlin-workbook
  • 7. Variables BASIC SYNTAX // Java final int a = 1; int b = 2; // Kotlin val a = 1 var b = 2 y/kotlin-workbook
  • 8. const BASIC SYNTAX y/kotlin-workbook ● Only for val ● Stricter than Java’s static ● Available only at top level and inside an object
  • 9. const BASIC SYNTAX const val ONE = 1 const val FOO = "FOO" const val FOO1 = "FOO" + ONE y/kotlin-workbook ● Only for val ● Stricter than Java’s static ● Available only at top level and inside an object
  • 10. ● Native suport for multiline strings ● All Strings literals are templates ● Templates are evaluated at compile-time ● Faster than String.format() Strings BASIC SYNTAX y/kotlin-workbook
  • 11. const val W = "World" "Hello $W!" val map = mutableMapOf("foo" to "bar") "foo is ${map["foo"]}" Strings BASIC SYNTAX y/kotlin-workbook
  • 12. val w = "World" println("Hello $w!") // prints "Hello World!" val map = mutableMapOf("foo" to "bar") println("foo is ${map["foo"]}") // prints "foo is bar" map["bar"] = "baz" println(""" foo is ${map["foo"]} bar is ${map["bar"]} """) Strings BASIC SYNTAX y/kotlin-workbook
  • 13. println("$1 $bills") // "$1 $bills" println("""$1 ${'$'}bills""") // "$1 $bills" Strings BASIC SYNTAX y/kotlin-workbook
  • 14. ● Can be declared at top level ● Can be declared inside other functions ● Can take other functions as parameters ● Can return functions functions BASIC SYNTAX y/kotlin-workbook
  • 15. fun world(): String { return "World" } functions BASIC SYNTAX y/kotlin-workbook
  • 16. fun world(): String { return "World" } fun world(): String = "World" functions BASIC SYNTAX y/kotlin-workbook
  • 17. fun world(): String { return "World" } fun world(): String = "World" fun world() = "World" fun hello(who: String) = "Hello $who" functions BASIC SYNTAX y/kotlin-workbook
  • 18. ● A single class, interface or object per file ● final is default modality ○ open and override keyword to declare extensibility ● public is default visibility ○ internal visibility instead of java default ● No new keyword class & interface BASIC SYNTAX y/kotlin-workbook
  • 19. interfaces BASIC SYNTAX interface Runnable { fun run() } y/kotlin-workbook
  • 20. interfaces BASIC SYNTAX interface Runnable { fun run() } interface RepeatingRunnable : Runnable { fun run(times: Int) { (1..times).forEach { run() } } } y/kotlin-workbook
  • 21. classes BASIC SYNTAX y/kotlin-workbook interface Runnable { fun run() } interface RepeatingRunnable : Runnable { fun run(times: Int) { (1..times).forEach { run() } } } open class Greeting : Runnable { override fun run() = println("Hi!") }
  • 22. classes BASIC SYNTAX y/kotlin-workbook interface Runnable { fun run() } interface RepeatingRunnable : Runnable { fun run(times: Int) { (1..times).forEach { run() } } } open class Greeting : Runnable { override fun run() = println("Hi!") } class RepeatingGreeting : Greeting(), RepeatingRunnable RepeatingGreeting().run(3)

Editor's Notes

  • #4: Compiler will optimize your code to use primitives if possible Unit exists and can be passed Unit replaces void in functions’ return types Nothing doesn’t exist and is closer to Java’s void
  • #5: Word of caution about aliases think very hard if you are really improving readability First one: normal import Second one: static import Third one: alias import
  • #6: Val instead of final
  • #7: Compiler always knows the type the right-hand-side of an assignment operator so you can drop it from the left-hand-side Do it only for obvious types
  • #8: Compiler always knows the type the right-hand-side of an assignment operator so you can drop it from the left-hand-side Do it only for obvious types
  • #11: Note: This is mostly about templates
  • #12: First one: Only use const values, so the whole thing can be created at compile time String templates which only use const templates can be evaluated at compile time Second string: Will be evaluated at runtime and use a String concatenation (these use StringBuilder under the hood)
  • #13: Template Strings: Preserve whitespace
  • #14: “Normal” strings can be escaped with dollar signs Multiline: need to use ${} blocks. Fortunately you’ll really only need to do that with $ and “, but if you need to do that
  • #15: Replacement for Java’s methods
  • #16: Need to include return type or it is considered Unit
  • #17: Normal and expression bodies Can only do this if something evaluates to a single value (i.e. it is an expression)
  • #18: Don’t need to include the type because you have an expression function
  • #19: Can put as many classes as you want in a file. We only keep one Explain open and override in more detail Open goes on the class and overrideable functions Override == the annotation. It gets treated as a compile time error not to have this New keyword: classes start with capital letter by convention
  • #22: Explain open for classes
  • #23: Explain override for classes and interfaces Briefly touch on the () after Greeting - more next session ALMOST END OF SLIDES - NEXT SLIDE IS LAST - Q&A STARTS