Sudeep's Blog

Learn Kotlin (Part 1) - Hello world and Basic Data Types

Post Series:

Part 1: Hello world and Basic Data Types

Part 2: Array, Collection and Range

Part 3: Control Flow

Part 4: Function

Part 5: Class

Since Kotlin is already a official programming language for Android Apps development, I have been learning Kotlin lately. In this post, i will take you through basic hello world skeleton program to data types, variables in Kotlin.

Hello World Program for Kotlin

fun main(args: Array) {
    println("Hello World!")
}

main is the entry point in the kotlin program.

Variables/Values in Kotlin

We can define either value or varaible in kotlin. So, what is the difference between these two declarations?

var a : Int = 3 vs val a : Int = 3

When you declare var then you can change the value later whereas if you declare val then you cannot change the value later. val makes the variable declaration immutable.

Data Types

kotlin provides data types like Java, but unlike the java data type starts with capital letter.

Numbers

val a : Int = 3;
val b : Float = 3.0f;
val d : Double = 4.9;
val l : Long = 3494949494949

Let us see what size of data can each type store.

Type Size
Byte 8
Short 16
Int 32
Long 64
Float 32
Double 64

Character and String

var a : Char = 'd'
val b : Char = 'e'
val name : String = "Sudeep"

Char can store one character whereas a String can store group of character.

Boolean

Like other programming languages, kotlin provides boolean data type which can store either true or false.

var isTrue : Boolean = false
var isFalse : Boolean = true

These are the basic data types in Kotlin, we have more complex data types such as Arrays, Collection and Ranges which we will discuss in future post.