Introduction
Hello guys. We already know that Google announced that Kotlin is a new first class language for Android Development. Kotlin is now an official language on Android. It's expressive, concise, and powerful. Best of all, it's interoperable with our existing Android languages and runtime.
You can try Kotlin by downloading Intellij idea Community Edition or using Kotlin Online Compiler provided by the Team JetBrains.
In this article, we will learn the basics of Kotlin like variables, constants, conditions, functions and etc. The following topics are to be covered in this article.
Functions
The following snippet shows the Hello World Program in Kotlin and it is similar to the Hello World Program in Java.
- fun main(args: Array <string>) {
- println("Hello, world!")
- }
Here the function is denoted by “fun” followed by function Name with passing arguments if needed then return type if the function returns data. The syntax of the Program is
- fun functionName(arguments: Datatype) : returnType {
- println("Hello, world!")
- }
Variables and Constants
The Following Snippet shows the How to use Variables and Constants with Datatype in Kotlin.
- Variables
Variables in Kotlin are denoted by the keyword “var” and the variables can be re-assigned as like in any programming languages.
- var a = 10
- a = a + 10
- println(a)
Constants
Constants in Kotlin are denoted by the keyword “val” and the constants cannot be re-assigned as like in any programming languages. If we did, then the Kotlin compiler will throw an error as “val cannot be reassigned”
- val x = 10
- println(x)
- var a = 10
- a = a + 10
- println(a)
Concate Variables or Constants
The variables or constants can be concatenated using plus symbol as with any programming language.
- fun main(args: Array<string>) {
- var a = 10
- a = a + 10
- println(a)
- val x = 10
- println(x)
- var str : String = "Android"
- println(str)
-
- println(str+"Mad")
- println("${str}Mad - Kotlin Series")
- }
The Datatype assigned by adding datatype followed by variable or constant's name.
- val x : Int = 10
- println(x)
Conditions
The Following Snippet shows the how to use conditions like if else, switch case in Kotlin.
- if else
If else conditions are similar to java. But, we can assign he output of the conditions as like ternary operator.
- fun main(args: Array<string>) {
- var arun = 10
- var dharun = 15
- var elder = if(arun > dharun)"Arun" else "Dharun"
-
- println("Elder is ${elder}")
- }
- When
Like Java, Kotlin doesn’t have switch statement. Instead of switch, it has when statement. The following statement shows the implementations of it.
- fun main(args: Array<string>) {
- var elder = "Dharun"
- when(elder){
- "Arun"->{
- println("Elder is Arun")
- }
- "Dharun"->{
- println("Elder is Dharun")
- }
- else->{
- println("No Answer")
- }
- }
- }
Loops
The Following Snippet shows the How to use loop conditions like for, while in Kotlin.
- For loop
The following code shows the example of “for loop” in Kotlin.
-
- fun main(args: Array<string>) {
- for(i in 1..10){
- println(i)
- }
- }
- While
The following code shows the example of “while loop” in Kotlin.
-
- fun main(args: Array<string>) {
- var x = 1;
- while(x<=10){
- println(x)
- x++
- }
- }
Arrays and Lists
The Arrays can be created by “arrayOf” keyword in Kotlin. It will accept any type like Object in Java. The following code shows its implementations.
- fun main(args: Array<string>) {
- var x = arrayOf(1,2,3,4,"Androidmads");
- for(i in x){
- println(i)
- }
- }
It can be controlled by Datatype as in Java. The following code shows its implementations.
- fun main(args: Array<string>) {
- var x = arrayOf<String>("Android","Mads","Kotlin","Series");
- for(i in x){
- println(i)
- }
- }
The Lists can be created by “listOf” keyword in Kotlin. It is similar to “arrayOf”. The following code shows its implementations.
- fun main(args: Array<string>) {
- var x = listOf<String>("Android","Mads","Kotlin","Series");
- for(i in x){
- println(i)
- }
- }
Classes
The Following code shows how to declare / create objects for class in Kotlin. For Example, Create a class and named it as "Model.kt"
- class Model {
- var name : String = ""
- }
The object call can be done by the following code.
- fun main(args: Array<String>) {
-
- var model = Model()
- model.name = "Androidmads"
- }
Constructors
The Following code shows how to create and call constructors in Kotlin. Create a Model class with the constructor as shown in below.
- class Model{
- constructor(name:String, age:Int) {
-
- }
- }
It can called as below.
- fun main(args: Array<String>) {
-
- var model = Model("Androidmads",20)
- println("${model.name}")
- }
Null Value Handling
In Java, Null value may lead to Null Pointer Exception. But, in Kotlin we have a Null Handling method to avoid this null pointer exception.
The Null Handling is done by using Nullable Values like in C#. For Example, Nullable String is denoted by “String?” The following code shows the usage Nullable values in Kotlin.
- fun main(args: Array<String>) {
- var name: String? = null
-
- println("${name}")
- }