Exploring Kotlin
Syntax, features & capabilites
Hani M K
This book is for sale at http://leanpub.com/exploring-kotlin
This version was published on 2019-11-30
* * * * *
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
* * * * *
2019 Hani M K
Thank you for downloading Exploring Kotlin.
This book is dedicated for those who spent a good amount of their career writing Java! Finally, here is a language that makes you be yourself :)
The first version of this book, is a result of individual effort. Writing this mini book was a good way to explore Kotlin. I hope reading it does the same thing too.
Your comments and suggestions are welcome.
Please, feel free to contact and follow if you like.
- Email: hmkcode@gmail.com
- Github: github.com/hmkcode
- Twitter: twitter.com/hmkcode
- Blog: hmkcode.com
Hani M. K.
CHAPTER 1 | Getting Started
- This chapter will cover the basic syntax of Kotlin and will introduce some of the tools used for coding in Kotlin.
1.1 | Tools
- There are many tools that will help you start coding in Kotlin.
- Tools listed below shall cover the needs for most Kotlin learners to start coding in Kotlin.
- Based on your goal from learning Kotlin, you may use one or more of these tools.
- In this book we will mainly be using Kotlin online playground.
Kotlin playground
- Kotlin Playground is an online sandbox to explore Kotlin.
- The playground is the easiest way to start exploring Kotlin.
1
fun
main
()
{
2
println
(
"Hello, world!!!"
)
3
}
IntelliJ IDEA
- IntelliJ IDEA is the preferred IDE for developing projects in Kotlin.
- Kotlin is bundled with IntelliJ IDEA. It has all what you need to start coding in Kotlin.
- You can download the free Community Edition
- You can follow the steps in this tutorial from Kotlin official site Getting Started with IntelliJ IDEA
Android studio
- Android Studio fully supports Kotlin.
- You can create a new project with Kotlin-first support, add Kotlin files to existing project with Java files or converting existing Java to Kotlin.
1.2 | Kotlin Basic Syntax
main()
function
- The starting point for running Kotlin app is
main()
function.
1
fun
main
()
{
2
println
(
"Hello world!"
)
3
}
val
& var
variables
- Read-only variables should be defined with
val
- Variables that can be updated or changed is defined as
var
- Variables must either have a type annotation or be initialized
1
val
w
:
Int
=
0
// immediate assignment
2
val
x
=
6
// type `Int` is inferred "no type provided"
3
val
y
:
Int
// Type is required "no initial value"
4
y
=
7
// deferred assignment "Type of y defined before"
5
val
z
=
3
// this is OK
6
7
z
=
z
+
1
// This is an error "Val cannot be reassigned"
8
9
var
counter
=
0
// this variable can be updated
10
counter
++;
Comments
- Comments can be added in Kotlin as following
1
// single line comment
2
3
/* Multi-line
4
comments */
String templates
- String template is a neat way to build a dynamic string.
1
fun
main
()
{
2
var
n
=
1
3
println
(
"n = $n"
)
4
val
result
=
"n is ${if(n > 0) "
positive
" else "
not
positive
"}"
5
println
(
result
)
6
}
Output:
1
n = 12
n is positive
- Notice: Kotlin expression inside the curly brackets.
Functions fun
- Functions is a block of reusable code that is used to perform an action and may return result.
- Functions are declared using
fun
keyword. - Functions may take variables or even other functions as parameters.
1
fun
multiply
(
a
:
Int
,
b
:
Int
):
Int
{
2
return
a
*
b
3
}
- Function with expression body and inferred return type can be written as:
1
fun
multiply
(
a
:
Int
,
b
:
Int
)
=
a
*
b
- Function parameters can have default values.
1
fun
multiply
(
a
:
Int
=
2
,
b
:
Int
=
4
)
=
a
*
b
- Function with no return value use
Unit
as return type which can be also omitted.
1
fun
printResult
(
a
:
Int
,
b
:
Int
):
Unit
{
2
3
println
(
"result = "
+
multiply
(
a
,
b
))
4
5
}
if
& when
Control Flow