A Beginner Tutorial To Kotlin – 1


  1. IntelliJ Idea or any other IDE that supports Kotlin syntax
  2. JVM (as Kotlin is a JVM based language)


JetBrains started project Kotlin in 2011. Kotlin is designed to be complied as fast as Java and having the features of Scala. Scala, as JetBrain points out, has a high compilation time. Kotlin has saveral variations that target the JVM (Kotlin/JVM), JavaScript (Kotlin/JS), and native code (Kotlin/Native). Kotlin is interoperable with Java. That means that we can have projects with both Kotlin and Java code.

New Project – Intellij

File -> New -> Project -> Kotlin (JVM | IDEA)

Your First Code – HelloWorld

In the src directory, right click and click on new Kotlin file, let’s call it helloWorld.kt

Write “main”, and IntelliJ will autocomplete the main function. This main is the start point of our Kotlin application. Function/method is represented by fun.

fun main() {
println("Hello World")
view raw helloWorld.kt hosted with ❤ by GitHub

Click the little play(green triangle button) near main. And the code would run.


The following gist has been well documented with example of functions in kotlin.

* firstParam is of the type Integer which is called Int
* secondParam is of the type Int
* The last Int after colon and before the opening bracket of method is the return type, which is Int in our case
fun multiply(firstParam : Int, secondParam : Int): Int {
return firstParam * secondParam
* This function is same as above except that we do not have
* to explicitly declare the return type
fun multiplyWithInferredReturnType(firstParam : Int, secondParam : Int) = firstParam * secondParam
* This function is same as above except that we have explicitly mentioned return type as Unit
* which means none.
fun multiplyWithUnitReturnType(firstParam : Int, secondParam : Int) : Unit {
println("${firstParam * secondParam}")
* This function is same as above except that we have not explicitly mentioned return type as Unit
* which means none.
fun multiplyWithNoReturnType(firstParam : Int, secondParam : Int) {
println("${firstParam * secondParam}")
fun main() {
println(multiply(10, 10))
view raw FunctionDemo.kt hosted with ❤ by GitHub


val is defined as read-only local variables.

var are the variables that can be reassigned.

val variable1: Int = 22  // variable1 is variable that is of Int type
val variable2 = 44 // variable2 is inferred as of type Int

Nullable values

Variables in Kotlin by default does not support null. Question mark after variable type says that the variable supports null. Following example will show the usage.

val can not be reassigned
Can not call with null value
fun main() {
val variable1 = 10
//variable1 = 20 //Illegal Expression because val can not be reassigned
var variable2 = 20
variable2 = 30
//printNum(null) // Illegal Expression because variable a is not nullable
fun printNum(a: Int){
// Notice the question mark after Int which represents that vara is nullable
fun printNullNum(vara: Int?){
view raw variableDemo.kt hosted with ❤ by GitHub





Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: