Table Of ContentKotlin
Kotlin is a free and open-source general-purpose programming language that is 
mostly used to create Android apps. Kotlin was developed by JetBrains initially 
and later picked up by Google as the language of choice for its Android platform. 
Over the past few years, Kotlin has become one of the most popular alternatives 
to Java language and is used by more than 80 percent of Android app developers.
Kotlin is a statically typed, advanced programming language that compiles Kotlin 
code into Java byte-code and runs on a Java Virtual Machine (JVM). It can also be 
compiled to native executables and JavaScript source code. It is an object-oriented 
programming language that includes data types, operators, I/O comments, control 
statements, functions, classes, object expressions, and constructors, among other 
features. It is not a standalone programming language; rather, is an innovative 
manner of coding that runs on Java.
Kotlin is a versatile language with some interesting and cool features like compat-
ibility, low runtime, and efficient coding characteristics. The features that help dis-
tinguish Kotlin from other languages are its reliability, tool support, and interoper-
ability. It is a simplified version of Java that is much easier to deal with.
Why Should You Learn Kotlin?
The popularity of Kotlin is growing, and it will continue to do so in the coming 
years. Kotlin has always been, and will continue to be, a cutting-edge program-
ming language that best meets the needs of real professionals. Many emerging 
fields, including mobile, online gaming, server-side, cloud development, data 
science, and education, are progressively using Kotlin.
All of this indicates that there is already a high demand for Kotlin developers, and 
that demand will continue to grow in the future. Furthermore, if you are looking to 
build native Android apps, Kotlin is the language that you should be using.
Kotlin
The Ultimate Guide
Edited by
Sufyan bin Uzayr
First edition published 2023
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
and by CRC Press
4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
CRC Press is an imprint of Taylor & Francis Group, LLC
© 2023 Sufyan bin Uzayr
Reasonable efforts have been made to publish reliable data and information, but the author and 
publisher cannot assume responsibility for the validity of all materials or the consequences of their use. 
The authors and publishers have attempted to trace the copyright holders of all material reproduced in 
this publication and apologize to copyright holders if permission to publish in this form has not been 
obtained. If any copyright material has not been acknowledged please write and let us know so we may 
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, 
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or 
hereafter invented, including photocopying, microfilming, and recording, or in any information 
storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.com 
or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 
978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.
co.uk
Trademark Notice: Product or corporate names may be trademarks or registered trademarks and are 
used only for identification and explanation without intent to infringe.
Library of Congress Cataloging-in-Publication Data
Names: Bin Uzayr, Sufyan, author. 
Title: Kotlin : the ultimate guide / Sufyan bin Uzayr. 
Description: First edition. | Boca Raton : CRC Press, 2023. | Includes 
bibliographical references and index. 
Identifiers: LCCN 2022025675 (print) | LCCN 2022025676 (ebook) | ISBN 
9781032311708 (hardback) | ISBN 9781032311692 (paperback) | ISBN 
9781003308447 (ebook) 
Subjects: LCSH: Android (Electronic resource) | Kotlin (Computer program 
language) | Application software--Development. | Mobile 
apps--Development. | Java (Computer program language) 
Classification: LCC QA76.73.K68 B56 2023 (print) | LCC QA76.73.K68 
(ebook) | DDC 005.1/14--dc23/eng/20220917 
LC record available at https://lccn.loc.gov/2022025675
LC ebook record available at https://lccn.loc.gov/2022025676
ISBN: 9781032311708 (hbk)
ISBN: 9781032311692 (pbk)
ISBN: 9781003308447 (ebk)
DOI: 10.1201/9781003308447
Typeset in Minion
by KnowledgeWorks Global Ltd.
Contents
Acknowledgments, xxi
About the Author, xxiii
Chapter 1   ◾   C  rash Course in Kotlin  1
KOTLIN PROGRAMMING LANGUAGE HAS A RICH HISTORY  2
FEATURES OF THE KOTLIN PROGRAMMING LANGUAGE  2
KOTLIN PROGRAMMING LANGUAGE APPLICATIONS  4
WHY SHOULD WE STUDY THE KOTLIN PROGRAMMING 
LANGUAGE? WHAT ARE THE BENEFITS OF LEARNING 
THE KOTLIN PROGRAMMING LANGUAGE?  4
DISADVANTAGES OF KOTLIN  5
WHAT IS THE PURPOSE OF KOTLIN?  5
Android Development  5
Back-End Web Development  6
Full-Stack Web Development  6
Data Science  6
Mobile Development for Several Platforms  7
KOTLIN’S ARCHITECTURE  7
KOTLIN FRAMEWORKS FOR SERVER-SIDE DEVELOPMENT  8
KOTLIN ENVIRONMENT FOR COMMAND LINE SETUP  9
Intellij IDEA IS USED TO BUILD UP A KOTLIN ENVIRONMENT  9
THE FIRST PROGRAM IS WRITTEN IN KOTLIN  11
KOTLIN DATA TYPES  12
Number Data Types in Kotlin  12
Character Data Type in Kotlin  13
v
vi   ◾   Contents
String Data Type in Kotlin  14
Boolean Data Type in Kotlin  14
Array Data Type in Kotlin  15
DATA TYPE CONVERSION IN KOTLIN  15
OPERATORS IN KOTLIN  16
Arithmetic Operators in Kotlin  16
Relational Operators in Kotlin  17
Assignment Operators in Kotlin  17
Unary Operators in Kotlin  18
Logical Operators in Kotlin  19
Bitwise Operations in Kotlin  19
BOOLEANS IN KOTLIN  20
Create Boolean Variables  20
Boolean Operators in Kotlin  21
Boolean Expression in Kotlin  21
and() and or() Functions in Kotlin  22
Boolean to String  22
STRINGS IN KOTLIN  22
String Templates in Kotlin  23
String Object in Kotlin  23
String Indexes in Kotlin  23
String Length in Kotlin  24
String Kotlin Last Index  24
String Case Changing  24
String Concatenation in Kotlin  25
Trim Characters from the String  25
Quotes Inside a String  26
Finding a String Inside a String  26
Comparing Two Strings  26
getOrNull() Function in Kotlin  26
toString() Function in Kotlin  27
Contents   ◾   vii
ARRAYS IN KOTLIN  27
Creating Arrays  27
Arrays of the Primitive Type  28
Elements of an Array Can Be Get and Set  28
Array Length in Kotlin  28
Loop through an Array  29
Check if an Element Exists  29
Distinct Values from the Array  29
Dropping Elements from the Array  30
Checking an Empty Array  30
RANGES IN KOTLIN  30
Creating Ranges Using the rangeTo() Function  31
Creating the Ranges Using the .. Operator  31
Creating the Ranges Using downTo() Operator  31
step() Function in Kotlin  32
Range of Characters in Kotlin  32
reversed() Function in Kotlin  32
until() Function in Kotlin  32
The last, first, and step Elements  33
Filtering Ranges  33
Distinct Values in Range  33
Range Utility Functions  34
FUNCTIONS IN KOTLIN  34
Built-in Functions in Kotlin  34
User-Defined Functions  34
Function Parameters  35
Return Values  35
Unit-Returning Functions  36
Recursive Function in Kotlin  36
Tail Recursion in Kotlin  37
Higher-Order Functions  38
viii   ◾   Contents
Lambda Function in Kotlin  38
Inline Function in Kotlin  38
If-else EXPRESSION IN KOTLIN  39
if Statement  39
if-else Statement  40
if-else Expression in Kotlin as the Ternary Operator  42
if-else-if Ladder Expression  42
nested if Expression  44
while loop IN KOTLIN  45
do-while loop IN KOTLIN  47
Method of do-while loop  47
for loop IN KOTLIN  48
Iterate across the Range Using a for loop  49
Using a for loop, Iterate over the Array  50
Iterate through a String Using the for loop  51
Iterate over the Collection Using the for loop  52
KOTLIN when expression  52
when to Use as a Statement with else  53
Using when as a Statement in the Absence of an else Branch  53
when Used as an Expression  53
In Kotlin, Different Ways to Use a when Block  54
UNLABELED BREAKS IN KOTLIN  57
In a while loop, Use of an Unlabeled Break  57
In a do-while loop, Use of an Unlabeled Break  58
Use of an Unlabeled Break in a for loop  59
LABELED BREAKS IN KOTLIN  60
In a while loop, Using a Labeled Break  60
In a do-while loop, Using a Labeled Break  61
Using a Labeled Break in a for loop  62
KOTLIN UNLABELED CONTINUE  63
Use of Unlabeled Continues in the while loop  63
Contents   ◾   ix
In a do-while loop, Use an Unlabeled Continue  64
Use of Unlabeled Continues in a for loop  64
KOTLIN LABELED CONTINUES  65
Use of Labeled Continues in a while loop  65
Use of Labeled Continues in a do-while loop  66
Use of Labeled Continues in a for loop  67
EXCEPTIONAL HANDLING  68
Exceptions in Kotlin  68
Exception Handling in Kotlin  69
Avoiding NullPointerException  70
What If We Fail to Deal with Exceptions?  70
How to Throw an Exception  70
KOTLIN try-catch block  71
The try-catch block as an Expression in Kotlin  72
The final block in Kotlin  72
Kotlin throw keyword  74
NESTED try block AND MULTIPLE catch block  74
Nested try block  74
Multiple catch block  75
NULL SAFETY  77
Nullable and Non-Nullable Sorts in Kotlin  77
Checking for the null in Conditions  78
Safe Call operator(?.)  79
Elvis Operator(?:)  80
Not null assertion: !! Operator  80
TYPE CHECKING AND SMART CASTING  81
Type Checking  81
Smart Casting  82
Use of  !is Operator  83
EXPLICIT TYPE CASTING  83
Unsafe Cast Operator: as  84
Safe Cast Operator: as?  85