SlideShare a Scribd company logo
Cosc 4730
Kotlin, a primer
Kotlin, a primer
• This is not expected to be a complete guide to Kotlin, See references
at the end
• It's to give you a flavor and be able to work with Kotlin within the
android framework.
• So somethings I'm covering, because android uses them, others I'm skipping,
because either I've never seen in android code or it's simply not used.
• Also, Kotlin is still new to me as well, so I'm still learning Kotlin too.
• Kotlin is also in places a functional language, using higher-order methods.
• It makes for less code in places, but also less clarity as what is actually happening.
Kotlin
• Formatting kotlin guide line.
• first the semicolons are optional, so line breaks are significant
• if you put to commands are the same line, you must use ; between them.
• You are supposed to use 4 space for indentation, never tabs
• For curly braces, put the opening brace in the end of the line where
the construct begins, and the closing brace on a separate line aligned
horizontally with the opening construct.
• The language design assumes Java-style braces, and you may encounter
surprising behavior if you try to use a different formatting style.
• https://kotlinlang.org/docs/reference/coding-conventions.html
Variables
• A variable is declared with either var or val
• var is mutable (standard variable in java or c++)
• val is immutable after initialization (constant in c++ or final in java)
• declaring
• val/var <name>: <Type> [= initial value]
• var a: String = "Hello Word" //variable a of type string with an initial value
• val b: Int = 1 //constant integer variable b with the value of 1
• val c = 3 //constant integer variable c with a value of 3, compiler determines
type
• var MyVar: Int //variable MyVar of type integer
• MyVar = 2 //set the value.
Variables types
• Numbers
• Byte, Short (16 bits), Int (32 bits), Long (64 bits), Float (32 bits), Double (64 bits)
• 123 is assumed a Int, 123L is a long
• 123.5 is assumed a double, 123F or 123.5F is a float.
• Characters
• Char
• Character literals use signal quotes, '1'
• Strings are an Array of type Char, like c++ and java.
• and standard escape sequences, like t, b, n, r, ', ",  and $
• Boolean
• Boolean, which has has two possible value: true and false
• operators like java/c++ || (or), && (and), ! (not)
Strings
• Unlike Java and C++, but more like some interpreted langauges (like perl and others)
• Strings can interpolate inside of a doublequote strings
• uses take the value of variable within the double quotes
• var myVar1: String = "Jim"
• var myVar2: String
• myVar2 = "My name is $myVar1" produces "My name is Jim"
• myVar2 = "$myVar1 is of length ${myVar1.length}" produces "Jim is of length 3"
• myVar2 = "$ stuff" produces "$ stuff" note you need to escape the $.
• Raw (or literal) strings use """ to denote start and stop.
val variable = """
hi there
"""
• last note, Raw strings use interpolation and don't support backslash escaping like $, so ${'$'} will
produce a $.
arrays
• arrays are declared like normal variables, it's about how they are
initialized.
• uses either arrayOf() or Array(size)
• So
• var myArray: Int = arrayof(1,2,3,4) creates an array of size 4 with
values 1,2 3,4. myArray[0] has the value of 1
• var myArray: Int = Array(3) creates an empty array of size 3
• Array also has a lambda expression constructor for the array
• Array(5, i -> i *2) produces an array of size 5, values [0, 2, 4, 6, 8]
arrays
• arrays are a class and have the following methods
• size (type Int) is the size of the array, (use size -1 to so don't overflow)
• get(index) return the value based on index or just use [index]
• set(index, value) or [index]= value
• there is also an iterator
• last note, there is also a special set of primitive arrays
• ByteArray, ShortArray, IntArray, etc. but likely just easier to use an Array.
collections (lists)
• like arrays, they have [] accessors, iterators, set, get operations
• like the java ArrayList, lists are dynamic sized
val list1: List<Int> = LinkedList<Int>() //linked list implementation
val list2: List<Int> = ArrayList<Int>()
• initializers.
val a = arrayOf(1, 2, 3)
a[0] = a[1] // OK
val l = listOf(1, 2, 3) //immutable
l[0] = l[1] // doesn't compile
val m = mutableListOf(1, 2, 3)
m[0] = m[1] // OK
m.add(4)
println(l.size) // 4
null safety
• In some languages, a reference type variable can be declared without providing an initial
explicit value. In these cases, the variables usually contain a null value. Kotlin variables
can't hold null values by default. This means that the following snippet is invalid:
• // Fails to compile
• val languageName: String = null
• For a variable to hold a null value, it must be of a nullable type. You can specify a variable
as being nullable by suffixing its type with ?, as shown in the following example:
• val languageName: String? = null
• With a String? type, you can assign either a String value or null to languageName.
• You must handle nullable variables carefully or risk a dreaded NullPointerException. In
Java, for example, if you attempt to invoke a method on a null value, your program
crashes.
• Kotlin provides a number of mechanisms for safely working with nullable variables.
Control Flow
• If, when, for, and while are the basic flow control.
• We'll look at each in turn, since they function much like java and in ways not at all like java
(actually more like c++).
• the If statement is actually an expression that returns value, so there is no ternary
operator (condition ? then : else)
• normal if with a single statement
if (a < b) c = 12
• with else and/or multiple statements the {} blocks are REQUIRED.
if (a < b) {
c = 12
} else {
c = 5
}
• It has the standard else if was well if () { … } else if () { } else {}
if as an expression.
• It looks like a ternary operator.
var c = if (a > b) 12 else 5
• since it's actually not a ternary operator we can do more, just has to have a return value
for each side
var c = if (a > b) {
d = "a >b"
12
} else {
d= "b > a"
5
}
switch/case statement, called when
• The when statement is a like a c++ switch
case statement, but no break statement is
needed. The when can be used like the if
as an expression that needed to return a
value.
when(x) {
1 -> c = 23
2 -> c = 10
else -> {
c = 0
d = 12
}
}
• you can use , for multiple value
0,1 -> statement
• in .. is the range operator, with the ! as
well
in 1 ..10 - > "in the range"
! in 11 .. 15 -> "no in range"
• or function/method calls as well, just
needs to return true or false
x.isOdd() -> c = "odd"
x.isEven() -> c = "even"
loops controls: while
• while loops work just like c++ and java
while(x>0) {
x--
}
• bottom testing, do .. while
do {
x--
} while (x >0)
• the break and continue operators are also supported in while (and for)
loops
loops controls: for
• the for is nothing like c++ and java. it's more like foreach in c# and python
• the for loop iterates through anything that provides an iterator.
var ints: int = arrayOf(1,2,3)
for (item in ints) {
…
}
for (x in 1 ..3) { ..}
• we can also use a downTo and step operators
for( x in 6 downTo 0 step 2) so 6, 4, 2, 0 are the values of x
loops controls: for (2)
• if you want to iterate with the index the array has a method indices
for (x in array.indices) {
// some statement with array[x]
}
• or use the withIndex method
for ( (index, value) in array.withIndex() ) {
println("the element at $index is $value")
}
loop control
• For more complex break, continue (and even a return) with labeling
of loops see
• https://kotlinlang.org/docs/reference/returns.html
functions
• basics:
• fun name([ parameters,]): ReturnType {
• statements
• return Type
• }
• parameters are optional and so is the return
fun generateAnswerString(countThreshold: Int): String {
val answerString = if (count > countThreshold) {
"I have the answer."
} else {
"The answer eludes me."
}
return answerString
}
fun generateAnswerString(countThreshold: Int): String = if
(count > countThreshold) {
"I have the answer"
} else {
"The answer eludes me"
}
functions (2)
• anonymous functions
• We'll see a lot of anonymous functions in android.
val stringLengthFunc: (String) -> Int = { input ->
input.length
}
• stringLengthFunc contains a reference to an anonymous function that
takes a String as input and returns the length of the input String as
output of type Int. For that reason, the function's type is denoted as
(String) -> Int.
• calling the above function is the same
• val stringLength: Int = stringLengthFunc("Android")
functions (3)
• higher-order functions, ie functions that take functions as parameters
• In java, this would be a way to use the callback interface.
fun stringMapper(str: String, mapper: (String) -> Int): Int {
// Invoke function
return mapper(str)
}
val name = stringMapper("Android", { input ->
input.length
})
class
•simple version with no
constructor.
class <name> {
variables
functions
}
•Note, there is not a
"new" keyword.
• example:
class Car {
val wheels = ArrayList<Wheel>()
//public and Wheel is another class.
private val doorLock: Int = 12
fun unlockDoor(key: Int): Boolean {
return key == doorLock
}
}
fun main() {
val car = Car() // construct a Car
val wheels = car.wheels
// retrieve the wheels value from the Car
}
class (2)
• constructors
class Car (val wheels: List<Wheel>) { //note it's declared here.
private val doorLock: Int = 12
fun unlockDoor(key: Int): Boolean {
return key == doorLock
}
}
val car = Car(ArrayList<Wheel>())
class members
• Constructors and initializer blocks
• Functions
• Properties
• Nested and Inner Classes
• Object Declarations
• Companion classes and variables.
• Think static keyword in java.
• All classes in Kotlin have a common superclass Any, that is the default
superclass for a class with no supertypes declared:
class Example // Implicitly inherits from Any
• Any has three methods: equals(), hashCode() and toString(). Thus, they are defined for all
Kotlin classes.
inheritance
• by default all kotlin class are final (ie can't be inherited)
open class Base //class can now be inherited
• to inherit (no constructor)
open class Base()
class Derived(): Base()
• with constructors
open class Base(p: Int)
class Derived(p: Int): Base(p)
• note a primary constructor must be initialized here.
inheritance (2): Overriding Methods
• things must be explicit in Kotlin. explicit modifiers for overridable
members and for overrides:
open class Shape {
open fun draw() { /*...*/ }
fun fill() { /*...*/ }
}
class Circle() : Shape() {
override fun draw() { /*...*/ }
}
• But fill() can not be overridden.
inheritance (2): Overriding variables.
open class Shape {
open val vertexCount: Int = 0 //set to zero, immutable
}
class Rectangle : Shape() {
override val vertexCount = 4 //set to 4, immutable
}
class Polygon : Shape (){
override var vertexCount: Int = 0 // Can be set to any number later
}
Interoperability
• One of Kotlin’s most important features is its fluid interoperability
with Java. Because Kotlin code compiles down to JVM bytecode, your
Kotlin code can call directly into Java code and vice-versa. This means
that you can leverage existing Java libraries directly from Kotlin.
Furthermore, the majority of Android APIs are written in Java, and
you can call them directly from Kotlin.
• https://developer.android.com/kotlin/learn#interoperability
Android and kotlin
• First, a Kotlin class can inherit a Java class.
• remember Kotlin is designed to be interoperable with java.
• part the pattern guide, which is used in the lecture and the next slide too.
• https://developer.android.com/kotlin/common-patterns
• Android's style guide, which android studio should also use
• https://developer.android.com/kotlin/style-guide
Side by side declaration of a fragment.
public class LoginFragment extends Fragment {
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_login, container, false);
usernameEditText = view.findViewById(R.id.username_edit_text);
passwordEditText = view.findViewById(R.id.password_edit_text);
return view;
}
}
class LoginFragment : Fragment() {
private lateinit var usernameEditText: EditText
private lateinit var passwordEditText: EditText
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?
): View? {
var view: View? = inflater.inflate(R.layout.fragment_login, container,
false)
usernameEditText = view?.findViewById(R.id.username_edit_text)
passwordEditText = view?.findViewById(R.id.password_edit_text)
return myView
}
}
lateinit allows late assignment in onViewCreated, if used before
initialized, Kotlin throws an Uninitialized PropertyAccessException.
? passes a null if needed, so it won't cause a null pointer
exception. (may not be what you want though !! requires a non
null value otherwise, it does through an exception.).
References
• https://developer.android.com/kotlin/getting-started-resources
• https://developer.android.com/kotlin/learn
• https://kotlinlang.org/
• https://kotlinlang.org/docs/tutorials/getting-started.html
• https://kotlinlang.org/docs/reference/basic-types.html
• https://kotlinlang.org/docs/reference/control-flow.html
• https://kotlinlang.org/docs/reference/classes.html
• https://kotlinlang.org/docs/reference/collections-overview.html
• https://play.kotlinlang.org/byExample/01_introduction/04_Null%20S
afety
Q A
&

More Related Content

PPTX
Should i Go there
PDF
Quick swift tour
PPTX
Presentation 2nd
PPT
Javascript
PPTX
Ruby basics
PPTX
OOP-java-variables.pptx
PDF
Computer science abot c topics array 2d array
PPTX
Introduction to Kotlin for Android developers
Should i Go there
Quick swift tour
Presentation 2nd
Javascript
Ruby basics
OOP-java-variables.pptx
Computer science abot c topics array 2d array
Introduction to Kotlin for Android developers

Similar to kotlin-nutshell.pptx (20)

PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PDF
Introduction to Python for Plone developers
PPSX
Java Tutorial
PPT
02basics
PPTX
Chapter-Five.pptx
PPTX
Android webinar class_java_review
PPTX
Review old Pygame made using python programming.pptx
PPT
learn JAVA at ASIT with a placement assistance.
PPTX
Groovy Programming Language
PPSX
Pointers
PDF
PDF
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
PPTX
Presentation 5th
PPTX
Review 2Pygame made using python programming
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPTX
Java best practices
PPT
fundamentals of JavaScript for students.ppt
PPT
Basics of Javascript
PPTX
PPTX
ScalaLanguage_ch_4_5.pptx
Dr.C S Prasanth-Physics ppt.pptx computer
Introduction to Python for Plone developers
Java Tutorial
02basics
Chapter-Five.pptx
Android webinar class_java_review
Review old Pygame made using python programming.pptx
learn JAVA at ASIT with a placement assistance.
Groovy Programming Language
Pointers
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
Presentation 5th
Review 2Pygame made using python programming
Esoft Metro Campus - Certificate in c / c++ programming
Java best practices
fundamentals of JavaScript for students.ppt
Basics of Javascript
ScalaLanguage_ch_4_5.pptx
Ad

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Teaching material agriculture food technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Ad

kotlin-nutshell.pptx

  • 2. Kotlin, a primer • This is not expected to be a complete guide to Kotlin, See references at the end • It's to give you a flavor and be able to work with Kotlin within the android framework. • So somethings I'm covering, because android uses them, others I'm skipping, because either I've never seen in android code or it's simply not used. • Also, Kotlin is still new to me as well, so I'm still learning Kotlin too. • Kotlin is also in places a functional language, using higher-order methods. • It makes for less code in places, but also less clarity as what is actually happening.
  • 3. Kotlin • Formatting kotlin guide line. • first the semicolons are optional, so line breaks are significant • if you put to commands are the same line, you must use ; between them. • You are supposed to use 4 space for indentation, never tabs • For curly braces, put the opening brace in the end of the line where the construct begins, and the closing brace on a separate line aligned horizontally with the opening construct. • The language design assumes Java-style braces, and you may encounter surprising behavior if you try to use a different formatting style. • https://kotlinlang.org/docs/reference/coding-conventions.html
  • 4. Variables • A variable is declared with either var or val • var is mutable (standard variable in java or c++) • val is immutable after initialization (constant in c++ or final in java) • declaring • val/var <name>: <Type> [= initial value] • var a: String = "Hello Word" //variable a of type string with an initial value • val b: Int = 1 //constant integer variable b with the value of 1 • val c = 3 //constant integer variable c with a value of 3, compiler determines type • var MyVar: Int //variable MyVar of type integer • MyVar = 2 //set the value.
  • 5. Variables types • Numbers • Byte, Short (16 bits), Int (32 bits), Long (64 bits), Float (32 bits), Double (64 bits) • 123 is assumed a Int, 123L is a long • 123.5 is assumed a double, 123F or 123.5F is a float. • Characters • Char • Character literals use signal quotes, '1' • Strings are an Array of type Char, like c++ and java. • and standard escape sequences, like t, b, n, r, ', ", and $ • Boolean • Boolean, which has has two possible value: true and false • operators like java/c++ || (or), && (and), ! (not)
  • 6. Strings • Unlike Java and C++, but more like some interpreted langauges (like perl and others) • Strings can interpolate inside of a doublequote strings • uses take the value of variable within the double quotes • var myVar1: String = "Jim" • var myVar2: String • myVar2 = "My name is $myVar1" produces "My name is Jim" • myVar2 = "$myVar1 is of length ${myVar1.length}" produces "Jim is of length 3" • myVar2 = "$ stuff" produces "$ stuff" note you need to escape the $. • Raw (or literal) strings use """ to denote start and stop. val variable = """ hi there """ • last note, Raw strings use interpolation and don't support backslash escaping like $, so ${'$'} will produce a $.
  • 7. arrays • arrays are declared like normal variables, it's about how they are initialized. • uses either arrayOf() or Array(size) • So • var myArray: Int = arrayof(1,2,3,4) creates an array of size 4 with values 1,2 3,4. myArray[0] has the value of 1 • var myArray: Int = Array(3) creates an empty array of size 3 • Array also has a lambda expression constructor for the array • Array(5, i -> i *2) produces an array of size 5, values [0, 2, 4, 6, 8]
  • 8. arrays • arrays are a class and have the following methods • size (type Int) is the size of the array, (use size -1 to so don't overflow) • get(index) return the value based on index or just use [index] • set(index, value) or [index]= value • there is also an iterator • last note, there is also a special set of primitive arrays • ByteArray, ShortArray, IntArray, etc. but likely just easier to use an Array.
  • 9. collections (lists) • like arrays, they have [] accessors, iterators, set, get operations • like the java ArrayList, lists are dynamic sized val list1: List<Int> = LinkedList<Int>() //linked list implementation val list2: List<Int> = ArrayList<Int>() • initializers. val a = arrayOf(1, 2, 3) a[0] = a[1] // OK val l = listOf(1, 2, 3) //immutable l[0] = l[1] // doesn't compile val m = mutableListOf(1, 2, 3) m[0] = m[1] // OK m.add(4) println(l.size) // 4
  • 10. null safety • In some languages, a reference type variable can be declared without providing an initial explicit value. In these cases, the variables usually contain a null value. Kotlin variables can't hold null values by default. This means that the following snippet is invalid: • // Fails to compile • val languageName: String = null • For a variable to hold a null value, it must be of a nullable type. You can specify a variable as being nullable by suffixing its type with ?, as shown in the following example: • val languageName: String? = null • With a String? type, you can assign either a String value or null to languageName. • You must handle nullable variables carefully or risk a dreaded NullPointerException. In Java, for example, if you attempt to invoke a method on a null value, your program crashes. • Kotlin provides a number of mechanisms for safely working with nullable variables.
  • 11. Control Flow • If, when, for, and while are the basic flow control. • We'll look at each in turn, since they function much like java and in ways not at all like java (actually more like c++). • the If statement is actually an expression that returns value, so there is no ternary operator (condition ? then : else) • normal if with a single statement if (a < b) c = 12 • with else and/or multiple statements the {} blocks are REQUIRED. if (a < b) { c = 12 } else { c = 5 } • It has the standard else if was well if () { … } else if () { } else {}
  • 12. if as an expression. • It looks like a ternary operator. var c = if (a > b) 12 else 5 • since it's actually not a ternary operator we can do more, just has to have a return value for each side var c = if (a > b) { d = "a >b" 12 } else { d= "b > a" 5 }
  • 13. switch/case statement, called when • The when statement is a like a c++ switch case statement, but no break statement is needed. The when can be used like the if as an expression that needed to return a value. when(x) { 1 -> c = 23 2 -> c = 10 else -> { c = 0 d = 12 } } • you can use , for multiple value 0,1 -> statement • in .. is the range operator, with the ! as well in 1 ..10 - > "in the range" ! in 11 .. 15 -> "no in range" • or function/method calls as well, just needs to return true or false x.isOdd() -> c = "odd" x.isEven() -> c = "even"
  • 14. loops controls: while • while loops work just like c++ and java while(x>0) { x-- } • bottom testing, do .. while do { x-- } while (x >0) • the break and continue operators are also supported in while (and for) loops
  • 15. loops controls: for • the for is nothing like c++ and java. it's more like foreach in c# and python • the for loop iterates through anything that provides an iterator. var ints: int = arrayOf(1,2,3) for (item in ints) { … } for (x in 1 ..3) { ..} • we can also use a downTo and step operators for( x in 6 downTo 0 step 2) so 6, 4, 2, 0 are the values of x
  • 16. loops controls: for (2) • if you want to iterate with the index the array has a method indices for (x in array.indices) { // some statement with array[x] } • or use the withIndex method for ( (index, value) in array.withIndex() ) { println("the element at $index is $value") }
  • 17. loop control • For more complex break, continue (and even a return) with labeling of loops see • https://kotlinlang.org/docs/reference/returns.html
  • 18. functions • basics: • fun name([ parameters,]): ReturnType { • statements • return Type • } • parameters are optional and so is the return fun generateAnswerString(countThreshold: Int): String { val answerString = if (count > countThreshold) { "I have the answer." } else { "The answer eludes me." } return answerString } fun generateAnswerString(countThreshold: Int): String = if (count > countThreshold) { "I have the answer" } else { "The answer eludes me" }
  • 19. functions (2) • anonymous functions • We'll see a lot of anonymous functions in android. val stringLengthFunc: (String) -> Int = { input -> input.length } • stringLengthFunc contains a reference to an anonymous function that takes a String as input and returns the length of the input String as output of type Int. For that reason, the function's type is denoted as (String) -> Int. • calling the above function is the same • val stringLength: Int = stringLengthFunc("Android")
  • 20. functions (3) • higher-order functions, ie functions that take functions as parameters • In java, this would be a way to use the callback interface. fun stringMapper(str: String, mapper: (String) -> Int): Int { // Invoke function return mapper(str) } val name = stringMapper("Android", { input -> input.length })
  • 21. class •simple version with no constructor. class <name> { variables functions } •Note, there is not a "new" keyword. • example: class Car { val wheels = ArrayList<Wheel>() //public and Wheel is another class. private val doorLock: Int = 12 fun unlockDoor(key: Int): Boolean { return key == doorLock } } fun main() { val car = Car() // construct a Car val wheels = car.wheels // retrieve the wheels value from the Car }
  • 22. class (2) • constructors class Car (val wheels: List<Wheel>) { //note it's declared here. private val doorLock: Int = 12 fun unlockDoor(key: Int): Boolean { return key == doorLock } } val car = Car(ArrayList<Wheel>())
  • 23. class members • Constructors and initializer blocks • Functions • Properties • Nested and Inner Classes • Object Declarations • Companion classes and variables. • Think static keyword in java. • All classes in Kotlin have a common superclass Any, that is the default superclass for a class with no supertypes declared: class Example // Implicitly inherits from Any • Any has three methods: equals(), hashCode() and toString(). Thus, they are defined for all Kotlin classes.
  • 24. inheritance • by default all kotlin class are final (ie can't be inherited) open class Base //class can now be inherited • to inherit (no constructor) open class Base() class Derived(): Base() • with constructors open class Base(p: Int) class Derived(p: Int): Base(p) • note a primary constructor must be initialized here.
  • 25. inheritance (2): Overriding Methods • things must be explicit in Kotlin. explicit modifiers for overridable members and for overrides: open class Shape { open fun draw() { /*...*/ } fun fill() { /*...*/ } } class Circle() : Shape() { override fun draw() { /*...*/ } } • But fill() can not be overridden.
  • 26. inheritance (2): Overriding variables. open class Shape { open val vertexCount: Int = 0 //set to zero, immutable } class Rectangle : Shape() { override val vertexCount = 4 //set to 4, immutable } class Polygon : Shape (){ override var vertexCount: Int = 0 // Can be set to any number later }
  • 27. Interoperability • One of Kotlin’s most important features is its fluid interoperability with Java. Because Kotlin code compiles down to JVM bytecode, your Kotlin code can call directly into Java code and vice-versa. This means that you can leverage existing Java libraries directly from Kotlin. Furthermore, the majority of Android APIs are written in Java, and you can call them directly from Kotlin. • https://developer.android.com/kotlin/learn#interoperability
  • 28. Android and kotlin • First, a Kotlin class can inherit a Java class. • remember Kotlin is designed to be interoperable with java. • part the pattern guide, which is used in the lecture and the next slide too. • https://developer.android.com/kotlin/common-patterns • Android's style guide, which android studio should also use • https://developer.android.com/kotlin/style-guide
  • 29. Side by side declaration of a fragment. public class LoginFragment extends Fragment { public LoginFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_login, container, false); usernameEditText = view.findViewById(R.id.username_edit_text); passwordEditText = view.findViewById(R.id.password_edit_text); return view; } } class LoginFragment : Fragment() { private lateinit var usernameEditText: EditText private lateinit var passwordEditText: EditText override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { var view: View? = inflater.inflate(R.layout.fragment_login, container, false) usernameEditText = view?.findViewById(R.id.username_edit_text) passwordEditText = view?.findViewById(R.id.password_edit_text) return myView } } lateinit allows late assignment in onViewCreated, if used before initialized, Kotlin throws an Uninitialized PropertyAccessException. ? passes a null if needed, so it won't cause a null pointer exception. (may not be what you want though !! requires a non null value otherwise, it does through an exception.).
  • 30. References • https://developer.android.com/kotlin/getting-started-resources • https://developer.android.com/kotlin/learn • https://kotlinlang.org/ • https://kotlinlang.org/docs/tutorials/getting-started.html • https://kotlinlang.org/docs/reference/basic-types.html • https://kotlinlang.org/docs/reference/control-flow.html • https://kotlinlang.org/docs/reference/classes.html • https://kotlinlang.org/docs/reference/collections-overview.html • https://play.kotlinlang.org/byExample/01_introduction/04_Null%20S afety
  • 31. Q A &

Editor's Notes

  • #4: https://kotlinlang.org/docs/reference/coding-conventions.html