DEV Community

Coder
Coder

Posted on • Originally published at itscoderslife.wordpress.com on

Android-Kotlin Digest #1

Planning to journal updates on my learning in Android and Kotlin development. This would be mostly from an ​ iOS developer’s perspective. I will be writing gist of what I learnt today and anything specific interesting.

To start off below are some things I learnt in Kotlin and differences with Swift.

Some basics of Kotlin:

var of mutable variables

val of immutable variables / constants

Adding variable value in a string "The value of variable is $variable_name"

Suffix ? for variables holding null values

Also has a javascript big arrow => function like syntax:

fun sum(a: Int, b: Int) : Int = a + b

Conditionals: if (obj is String) { … } and not checks if (obj !is String) { … }

I found switch statements most unique till now:


when (obj) {
1 -> "One" // (case)
"Hello" -> "Greeting" // (case)
is Long -> "Long" // (case)
!is String -> "Not a string" // (case)
else -> "Unknown" // (default case)
}

Ranges are specified as 0..end or 0..end+1 or 0..list.lastIndex

Then we have progressions:

for (x in 1..10 step 2) {
print(x)
}
Jumps alternate steps with keyword step and value 2. Here result would be: 13579

for (x in 9 downTo 0 step 3) {
print(x)
}


Jumps three steps but in reverse order with keyword downTo. Here result would be: 9630

Top comments (0)