Kotlin Fundamental - pptx-2
Kotlin Fundamental - pptx-2
Fundamental
How to write Kotlin code
Kotlin Programming
This presentation is protected by Indonesian copyright
Thislaws. Reproduction
presentation is protected byand Distribution
Indonesian of Reproduction
copyright laws. the presentation without
and Distribution of the written permission
presentation without written of the author
permission is prohibited.
of the author is prohibited.
Why efficient?
What are Fundamentals?
https://medium.com/purwadhikaconnect/haruskah-belajar-coding-dari-
fundamental-bcaf434b032b
value
name
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Variable in Kotlin
In Kotlin variables will require the keywords var or val, identifier, type and initialization.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Variable in Kotlin
In Kotlin variables will require the keywords var or val, identifier, type and initialization.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Variable in Kotlin
Kotlin supports type inference so we are allowed to not write data types explicitly.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Let's try to code
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Let's try to code
1
2
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Any question?
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Character
Char type variable define can use single quotes (' ')
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Character
Increment (++) and decrement (--) operations on the Char data type
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : String
Not only Char, the String data type is also a text value. The difference is, String can hold
several characters in it.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : String
Basically a set of characters in a String value is in the form of an Array, so we can retrieve a
single character by using indexing.
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : String
Basically a set of characters in a String value is in the form of an Array, so we can retrieve a
single character by using indexing.
What is Indexing?
Indexing is an easy way to get elements in a collection by using the index or position of the
element. The position of an element starts from 0.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : String
Kotlin have two kinds of string, Escaped String and Raw String.
Escaped string is declared within double quote (" ") and may contain escape characters like:
Escaped string is declared within double quote (" ") and may contain escape characters like \t,
\n, \', \", \\.
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : String
Raw string is declared within triple quote (""" """) and Let’s code…
may contain multiple lines of text without any escape
characters.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Any question?
Boolean is a data type that has only two values, true and false. There are 3 (three) operators
that can be used in Boolean.
&& Logical and Returns true if both operands are true x && y
! Logical not Reverse the result, returns false if the operand is true !x
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Boolean
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Boolean Expression
> <=
< ==
>= !=
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Boolean Functions
Kotlin provides and() and or() functions to perform logical AND and logical OR operations
between two boolean operands.
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Numbers
Number data types are used to define variables which hold numeric values.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Numbers
Each Number data type has a different size (bit unit), depending on the amount of value that
can be stored. As shown in the following table:
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Any question?
Array is a data type that can store multiple values or objects in a variable.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types : Array
Array is a data type that can store multiple values or objects in a variable.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Get and Set the Elements of an Array
Use the index number inside square brackets ([ ]) for access an array element. Array index
starts with zero (0). So if access 1th element of the array then give number 0 as the index.
Kotlin provides get() and set() member functions to get and set the value at a particular index.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Get and Set the Elements of an Array
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Primitive type Arrays
Kotlin also has built-in factory methods to create arrays of primitive data types.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Any question?
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Functions : Create a function
return a value
function arguments
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Functions
Let’s to code
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
If Expressions
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
If Statement
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
if…else Expression
If in Kotlin can also be used as an expression. Let's try using an if statement that can return a
value to a variable.
Why is it called an IF expression? because the If statement that returns a value and is stored
in a variable
And now let's try for if...else expression:
if…else Expression
We can use else if condition to specify a new condition if the first condition is false.
Because it is very common and can be fatal, NPE is known by the term above.
NullPointerException (NPE) is an error that occurs when accessing or managing the value of an
uninitialized variable or a null value variable.
Kotlin comes with easy nullability handling that minimizes the occurrence of
NullPointerExceptions.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Nullable Types
Kotlin is able to distinguish between null and non-null objects when they are created.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Nullable Types
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Any question?
?
ER
RO
R
Handle nullable objects in an easier way using Safe Calls and Elvis Operators.
?
ER
RO
R
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Safe calls & Elvis Operator
To use a safe call, replace the ( . ) with ( ?. ) sign when accessing or managing values from
nullable objects.
By implementing the safe call as above, the compiler will skip the process if the object is null.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Safe calls & Elvis Operator
The Elvis operator allows you to set a default value if the object is null.
By adding a ( ?: ) at the end of the object value that has used Safe Call, then continued by
writing the default value.
code it!
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Safe calls & Elvis Operator
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
String Template
String Template is a feature that allows to insert variables into String without concatenation
(merging String objects using +)
variable
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
String Template
Let’s code…
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
String Template
Kotlin also makes it possible to insert expressions into template strings. By inserting the
expression into curly braces followed by the $ character.
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Thank You!
Kotlin Programming
This presentation is protected by Indonesian copyright
Thislaws. Reproduction
presentation is protected byand Distribution
Indonesian of Reproduction
copyright laws. the presentation without
and Distribution of the written permission
presentation without written of the author
permission is prohibited.
of the author is prohibited.
https://kotlinlang.org/docs/home.html
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm
https://www.tutorialspoint.com/kotlin/kotlin_data_types.html
https://developer.android.com/codelabs/basic-android-kotlin-compose-variables#4
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Font : SF Compact (Keynote) & Inter (Google slides)
Illustrasi : https://storyset.com
Color Pallette :
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.