-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering Kotlin for Android 14
By :

In this section, we’ll be looking at Kotlin syntax and familiarize ourselves with the language. Kotlin is a strongly typed language. The type of a variable is determined at the time of compilation. Kotlin has a rich type system that has the following types:
String?
. Non-nullable types are normal types without any operator at the end – for example, String
.class
keyword and you can add methods, properties, and constructors.val shortArray = ShortArray(10)
The following shows how you define object arrays:
val recipes = arrayOf("Chicken Soup", "Beef Stew", "Tuna Casserole")
Kotlin automatically infers the type when you don’t specify it.
Enum
keyword for you to declare enumerations.(Boolean) -> Unit
shorthand notation. This example takes a Boolean
argument and returns a Unit
value.We’ve learned the different types available in Kotlin, and we’ll use this knowledge in the next section to define some of these types.
Follow these steps to create your first Kotlin project:
Figure 1.1 – New Project dialog
Let’s work through the options in the dialog shown in Figure 1.1 as follows:
chapterone
.
Figure 1.2 – Project structure
The IDE creates the project with the project structure seen in Figure 1.2. We are mostly interested in the src/main/kotlin
package, which is where we’ll add our Kotlin files.
src/main/kotlin
package.Main
. The IDE will generate a Main.kt
file.Now that we’ve created our first Kotlin project and added a Kotlin file, in the next section, we will create functions in this file.
In Kotlin, a function is a block of code that does a specific task. We use the fun
keyword to define functions. Function names should be in camel case and descriptive to indicate what the function is doing. Functions can take arguments and return values.
Create the main()
function in your Main.kt
file as follows:
fun main() { println("Hello World!") }
In the preceding code, we’ve used the fun
keyword to define a function with the name main
. Inside the function, we have a println
statement that prints the message "
Hello Word!"
.
You can run the function by pressing the green run icon to the right of the function. You’ll see the console window pop up, displaying the message "
Hello World!"
.
We’ve learned how to create functions and print output to the console. In the next section, we’ll learn how to create classes in Kotlin.
To declare a class in Kotlin, we have the class
keyword. We’re going to create a Recipe
class as follows:
class Recipe { private val ingredients = mutableListOf<String>() fun addIngredient(name: String) { ingredients.add(name) } fun getIngredients(): List<String> { return ingredients } }
Let’s break down the preceding class:
Recipe
and it has no constructor.ingredients
, which is a MutableList
of Strings. It’s mutable to allow us to add more items to the list. Defining variables in a class like this allows us to be able to access the variable anywhere in the class.addIngredient(name: String)
, which takes in a name as an argument. Inside the function, we add the argument to our ingredients list.getIngredients()
function, which returns an immutable list of strings. It simply returns the value of our ingredients list.To be able to use the class, we have to modify our main function as follows:
fun main() { val recipe = Recipe() recipe.addIngredient("Rice") recipe.addIngredient("Chicken") println(recipe.getIngredients()) }
The changes can be explained as follows:
Recipe
class and assign it to the recipe
variableaddIngredient
method on the recipe
variable and pass in the string Rice
addIngredient
method on the recipe
variable and pass in the string Chicken
getIngredients
method on the recipe
variable and print the result to the consoleRun the main function again and your output should be as follows:
Figure 1.3 – Recipes
As you can see from the preceding screenshot, the output is a list of ingredients that you added! Now you can prepare a nice rice and chicken meal, but in Kotlin!
Kotlin has tons of features and we’ve barely scratched the surface. You can check out the official Kotlin documentation (https://kotlinlang.org/docs/home.html) as well to learn more. You’ll also learn more features as you go deeper into this book.
We’ve learned how to create classes, define top-level variables, and add functions to our class. This helps us understand how classes in Kotlin work. In the next section, we will learn how to migrate a Java class to Kotlin and some of the tools available to use in the migration.