Basics of Kotlin

Reading Time: 2 minutes

Hello everyone today I will discuss the basics of kotlin for example Hello world program, variables, functions, if and when, functions. So let’s do some practice and also important some theory.

Hello World

package intro
fun main() { // from kotlin 1.3 program arguments are optional
        val name = "kotlin" // val for final variable
        println("Hello, $name!!!!")
}

Things to be noticed if we compare with Java:
– by looking at the example we are sure that we write the methods with fun keywords.
– There is no class there or we can say we define the function at the package level, so the main function is only defined there, these types of functions we called top-level functions.

Main with arguments

fun main(args: Array<String>) { // there is no built-in syntax for arrays, Array is a ragular class with type String 
        val name = if(args.size >0) args[0] else "kotlin" //In kotlin if is an expression
        println("Hello, $name!!!!")
}

Variables in Kotlin

To declare a variable in Kotlin, either var or val keyword is used. Lets take a look:

var medium = "English"
val rollNumber = 21
  • Here, the medium is a variable of type and rollNumber is a variable of type Int.
  • You don’t have to specify the type of variables; Kotlin implicitly does that for you.
  • The compiler knows this by initializer expression (“English” is a String, and 21 is an integer value in the above program). This is called type inference in programming.
  • We have initialized variables during declaration.
  • You can declare a variable and specify its type in one statement, and initialize the variable in another statement later in the program.

Val or Var

  • The difference in using var and val is that var is mutable and val is immutable. For now, let’s focus on variable declaration.

Types of functions in kotlin

  • Local functions
  • Member functions
  • Generic functions
  • Inline functions
  • Extension functions
  • Higher-order functions and lambdas
  • Tail recursive functions

Functions in Kotlin are declared using the fun keyword:

fun double(x: Int): Int {
    return 2 * x
}

If Expression and when

If Expression

In Kotlin, if is an expression, i.e. it returns a value. Therefore there is no ternary operator (condition ? then: else), because ordinary if works fine in this role.

val max = if( a > b) a else b
//So here if is an expression that return the value.
//There is no ternary operator in Kotlin like Java:
(a > b) ? a:b

when as switch
The when expression replaces the switch statement in C-like languages. In the simplest form, it looks like this

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}
Screenshot-from-2021-01-06-23-17-12

Summary

After reading this blog you are able to write hello world program and having some basic knowledge of if and when, types of functions in kotlin, the difference between val and var, and last but not least what is type inference.

References

https://kotlinlang.org/docs/reference/control-flow.html
https://www.programiz.com/kotlin-programming/when-expression
https://www.javatpoint.com/kotlin-when-expression

Written by 

Shivraj Singh is a senior software consultant having experience of more than 2+ years. On the personal hand, he likes eating food and playing video games like NFS. He is always ready to learn new technology and explore new trends in the IT world. Shivraj is familiar with programming languages such as Java, Scala, C, C++ and he is currently working on reactive technologies like Scala, Akka, and Kafka.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading