Getting Started with Kotlin – Part 1

I have been building Android Apps for over  5 years now. Started off in java and now a big shift to Kotlin took me quite some time to get accustomed to it. So here, I’m sharing some nutshell of Getting started with Kotlin.

So we all know why Kotlin, if not here’s a link. If you want me to cover that do let me know as well. However, when I saw all the hype Kotlin created after being announced the official language of Android Development. I wanted to get into the nitty-gritty stuff that to say the language itself. So if I’m about to shift my existing knowledge better start from scratch right?

So here, I’ll share the basics. The language itself.



Kotlin has two types of variables ie val and var.

val: Use this if you want to define an immutable variable, a constant.

val firstName: String = "Shilu"
val lastName = "Shrestha"

Notice how we don’t explicitly declare the type of  lastName. Kotlin is a strongly typed language that supports type inference or deduction. The compiler handles type inference.

var: Use this to declare a variable.

var address = "Kathmandu, Nepal"
// later
address = "Lalitpur, Nepal"
// however, we can't do this
address = 123456

Using val the value of the variable can change, but its type cannot.


Strings are similar to java however, a lot powerful than in java. Strings can be declared using a double quotes or triple but not single which is used in Character types.

val message = "Hello, world!\n"

// using triple quote
val text = """ 
 Dear Readers,
   Start of new line here. 

One cool feature that I loved with Strings in Kotlin is String templates and String Interpolation.

var first = "Shilu"
var last = "Shrestha"
val str = "Hello $first $last"

// add logics and even an arbitrary expression 
println("Wow you have a ${if(first.length > 7) "long" else "short"} name")
// how cool is that!



Numbers are some-wart similar to Java.

  • Numbers
    • Long – 64 bit
    • Int – 32 bit
    • Short – 16 bit
    • Byte – 8 bit
  • Floating
    • Double – 64 bit
    • Float – 32 bit

You can use underscores to make number constants more readable:

val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010

Every number type supports the following conversions:

  • toByte(): Byte
  • toShort(): Short
  • toInt(): Int
  • toLong(): Long
  • toFloat(): Float
  • toDouble(): Double
  • toChar(): Char
val age = "45"
if(age.toInt() > 50){
// do something

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s