Get to Know with Kotlin & Android!

Aya Aurora
5 min readApr 20, 2020

For the past two days, I fortunately had the chance to learn Kotlin and Android in Gojek Bootcamp. Actually, the materials that I learnt was really compact yet it was easy to follow since the coaches were able to deliver it very well.

Today, I would discuss about the interesting concepts of Kotlin and Android that I found by learning those two within two days. This post might not cover all the Kotlin and Android materials, but still, I found lots of interesting concepts that I hope even tho this post would be pretty long, you, readers, could still enjoy reading it since Kotlin has lots of cool stuffs that you must check it out firsthand!

Kotlin

First fun fact about Kotlin, it is named after an island in Russia. Well, I wasn’t surprise since its predecessor language (read: Java) was also named after an island.

More about Kotlin, it is a functional and object oriented programming. Doesn’t it already interest you? Without further a do, let’s delve more into Kotlin’s enticing concepts! :D

Val and Var

As you might guess, this sub topic is about variables in Kotlin. To make things short, Kotlin has var to declare mutable variables and val to declare…

immutable variables?

Nope! val is used to declare read-only variables, meaning that variable could only be assigned once.

I believe, you might be asking,

then, what’s the different with immutable variable? Isn’t it only able to assigned once too?

Actually, there’s a little difference between read-only and immutable variable. Unlike read-only variable, the immutable object’s state can not be change at all. In Kotlin, immutable variables are declared with const val.

Delegated Properties

Actually, to me, understanding delegated properties in Kotlin wasn’t really that easy. It took me couple of re-reading this material in Kotlin Docs. But here is what I understand about it.

From what I learnt, Kotlin has two ways of declaring delegated properties:

  1. lazy properties
  2. observable properties

In lazy properties, the value gets computed only upon first access. For example, from the simple code snippet below,

val lazyValue: String by lazy {
println("computed!")
"Hello"
}
fun main() {
println(lazyValue)
println(lazyValue)
}

the result will be

computed! Hello
Hello

as you can see, the value Hello was only assigned once at the very first time variable lazyvalue is accessed.

As for the observable properties, there are two ways for you to declare the variables. First, you create the class that will handle the delegate’s job. For instance,

class Example {
var p: String by Delegate()
}

the variable p in Example Class delegates its getter and setter to Delegate Class. Within the Delegate Class, you need to write getValue and setValue functions. For better understanding about this example please check this link.

Second, you can declare the variable like this

import kotlin.properties.Delegatesclass User {
var name: String by Delegates.observable("<no name>") {
prop, old, new ->
println("$old -> $new")
}
}
fun main() {
val user = User()
user.name = "first"
user.name = "second"
}

from that instance, the result will be

<no name> -> first
first -> second

as you can see, with that way of delegated properties declaration, we can access the previous value of the variable name .

Extension Function

In Kotlin, we are allowed to add a function to the existing class with extension! By that, we don’t need to inherit the existing class in order to implement a function. Pretty neat, ‘aight? Here are the usage example.

fun Double.roundTo(n: Int): Double {
return "%.${n}f".format(this).toDouble()
}

From the example, the function roundTo is added to Double Class in order to round the value into n fractional digits.

Data Class

One reason why I consider myself converting to be a Kotlin developer is because of this feature that Kotlin has!!! Let’s say NO to tedious declaring POJO class in Java and please welcome data class in Kotlin!

In Kotlin, whenever you want to declare a POJO class, you only need to write data in front of it. It’s really as simple as this

data class Animal(val name: String, val age: Int)

And voîla! You get a class with its getter, setter, hashCode, equals, and toString with only that one single line!!!

*fangirling Kotlin already*

Trailing Lambda

There is a convention in Kotlin where we can place the last parameter of a function outside the parentheses if that parameter is a function. Aren’t you confused yet? Check out this example

fun operateCalc(firstOperand: Int, secOperand: Int, op: (Int, Int) -> Int) {
return op(firstOperand, secOperand)
}
operateCalc(2, 2) { first: Int, sec: Int ->
first + sec
}

this syntax is known as trailing lambda.

Typealias

Kotlin has typealias to allow the developers to use alternative names for existing type. For better understanding, the example is shown as follow

typealias Conditional<T> = (T) -> Booleanval evenFilter: Conditional<Int> = { it % 2 == 0 } 

As you can see, the (T) -> Boolean function signature is renamed as Conditional<T> and it expresses more intent of its function.

Android

Here are the basic knowledge that I learnt about Android. I believe this is a must-know materials for a beginner of android developer like me :)

Activity and Its Lifecycle

An activity is an entry point for interacting with the user. It represents a single screen with a user interface.

From that activity definition in mind, there is a question pops out in my mind

How can we manage or know when the activity will appear and disappear?

Activity itself has its own lifecycle. It’s simply explained and visualised with the picture below

Actually, the activity lifecycle is way more complex than the picture shown above. However, as the newbie of android developer, knowing those given cycles above is a must thing.

UI Layout

Android has several ways to manage the layout of its content. Several layouts that I learnt and understand clearly are

  1. ContraintLayout
  2. RelativeLayout
  3. LinearLayout

As clear as its name, the constraint layout gives the flexibility of positioning and changing size of the child view/widgets, the relative layout allows you to display the child view in relative position, and the linear layout allows you to align the child view in a single direction (vertically or horizontally).

That’s it. That’s almost wrapped all the materials of Kotlin and Android that I learnt for two days! Hope you find this post interesting and triggers you more to learn Kotlin and Android :D

--

--