0% found this document useful (0 votes)
156 views64 pages

Kotlin Fundamental - pptx-2

This document discusses Kotlin fundamentals including variables, data types, and strings. It covers variable declaration using val and var, built-in data types like numbers, booleans, characters and strings. It also discusses string indexing and escaped vs raw strings.

Uploaded by

Siti Nurhidayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views64 pages

Kotlin Fundamental - pptx-2

This document discusses Kotlin fundamentals including variables, data types, and strings. It covers variable declaration using val and var, built-in data types like numbers, booleans, characters and strings. It also discusses string indexing and escaped vs raw strings.

Uploaded by

Siti Nurhidayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Kotlin

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?

? At the end of the framework,


why learn fundamentals?

Should only use the


fundamentals?

https://medium.com/purwadhikaconnect/haruskah-belajar-coding-dari-
fundamental-bcaf434b032b

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of


the presentation without written permission of the author is prohibited.
Table of Content

● Variable & Data types ● Nullable Types

● Functions ● Safe calls & Elvis Operator

● If Expressions ● String templates

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of


the presentation without written permission of the author is prohibited.
Variable & Data types

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of


the presentation without written permission of the author is prohibited.
Variable

Variables are an important part of any programming.

A variable is a name that is entered into a computer memory location that is


used to store a value in a computer program, then the name is used to
retrieve the stored value and use it in the program.

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.

val or var identifier Type 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.

val Cannot change the value that was previously initialized.

var Can change the initialized value.

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

Data type is a data classifier based on the type of data


owned by the variable

The data type also determines the operations that can


be performed on a variable and how the value of a
variable is stored

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Data Types

Data type is a classification of data based on the type of data.

Data Types Description Example

Character Just one character 'A'

String Just Text "Kampus Merdeka"

Boolean Just two values true / false

Numbers Double, Long, Int, Short, Byte 128, -15, 2.7182818284

Array Save multiple objects arrayOf(2, 4, 6, 9, "Infinite Learning" , true)

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 is a data type whose value type is text.

Characters type are represented using the Char.

Char type variable define can use single quotes (' ')

Char can only be used to store single characters

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.

String data type are represented using the String.

Defines a variable with double 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 : 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:

\t : add tabs to the text.


\n : create a new line in the text.
\' : adds a single quote character to the text.
\" : adds a double quote character to the text.
\\ : adds a backslash character to the text.
Data Types : String

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?

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

Boolean is a data type that has only two values, true and false. There are 3 (three) operators
that can be used in Boolean.

Operator Name Description Example

&& Logical and Returns true if both operands are true x && y

|| Logical or Returns true if either of the operands is 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

Boolean variable mostly used with checking Let’s code…


conditions with if...else expressions. A
boolean expression makes use of relational
operators, for example:

> <=

< ==

>= !=

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.

Some of the default types that represent Numbers


are Byte, Short, Int, Long, Float, and Double.

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:

Data Type Size (bits) Data Range

Byte 8 bit -128 to 127

Short 16 bit -32768 to 32767

Int 32 bit -2,147,483,648 to 2,147,483,647

Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38

Double 64 bit 4.94065645841246544e-324 to 1.79769313486231570e+308


Data Types : Numbers

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?

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.

Arrays in Kotlin are represented by the Array.

To create an Array, we can use a library function arrayOf()

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.

Arrays in Kotlin are represented by the Array.

To create an Array, we can use a library function arrayOf()

Optionally with provide a data type as follows:

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.

Get the value Set the value

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.

intArrayOf() : IntArray longArrayOf() : LongArray

booleanArrayOf() : BooleanArray shortArrayOf() : ShortArray

charArrayOf() : CharArray byteArrayOf() : ByteArray

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

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of


the presentation without written permission of the author is prohibited.
Functions

A function is a procedure that is related to messages and


objects

Functions are created to perform specific tasks

A function can be said to be a mini program that will run


when called

Reusable and decomposition

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

Function declarations with the keyword fun

Continue the name of the desired function

Setting parameters (optional)


- Parameter name
- Tipe parameter separated by ( : )
- Each parameter of a function is separated by a comma ( , )

Specify the return type of the function (optional)

Function body is inside { } (in which there is an expression or


statement to run)
This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Functions : Structure

function name parameter name parameter type


fun Keyword
return type

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 Expressions

If expression is used when initializing the value of a variable based on a condition.

If expression is represented by the keyword if.

if is used to test a condition to run a process.

if will execute a statement or expression if the evaluation


result in the if block is true. Otherwise, it will be passed if it
evaluates to false.

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
If Statement

Let's start with traditional if...else statement.

Why is it called an If statement? because If doesn't return any value, it's a


command to print the data to the screen. (Just branching)

Come on, try the code!

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.

Let’s code it!


Any question?

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 makes it easy to manage nullable


variables.

Kotlin is able to distinguish between null and


non-null objects.

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of


the presentation without written permission of the author is prohibited.
Nullable Types

“The Billion Dollar Mistake”

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.

Add sign ? after specifying the object type so it can be null

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.
Nullable Types

Only safe (?.) or non-null asserted (!!.)


calls are allowed on a nullable receiver of
type String?

Check if the object is null or not.

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.
Safe calls & Elvis Operator

?
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

Handle nullable objects in an easier way using Safe Calls and Elvis Operators.

Safe Call will guarantee that the code we write is


safe from NullPointerException.

?
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!

Elvis will return the default value ( 0 ) if safeTextLength variable 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

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

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…

Without String Template Using String Template

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.

Let’s code it!

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 :

#F7C049 #448880 #000000

This presentation is protected by Indonesian copyright laws. Reproduction and Distribution of the presentation without written permission of the author is prohibited.

You might also like