SlideShare uma empresa Scribd logo
1 de 31
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
&

Mais conteúdo relacionado

Semelhante a kotlin-nutshell.pptx

Semelhante a kotlin-nutshell.pptx (20)

Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
02basics
02basics02basics
02basics
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
 
Pointers
PointersPointers
Pointers
 
Java part 2
Java part  2Java part  2
Java part 2
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Java best practices
Java best practicesJava best practices
Java best practices
 
Basics of Javascript
Basics of JavascriptBasics of Javascript
Basics of Javascript
 
Aspdot
AspdotAspdot
Aspdot
 
ScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptxScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptx
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
ITFT - Java
ITFT - JavaITFT - Java
ITFT - Java
 
Programming fundamental 02.pptx
Programming fundamental 02.pptxProgramming fundamental 02.pptx
Programming fundamental 02.pptx
 
Java introduction
Java introductionJava introduction
Java introduction
 

Último

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

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 &

Notas do Editor

  1. https://kotlinlang.org/docs/reference/coding-conventions.html