Kotlin introduction

Pankaj Rai 🇮🇳
AndroidPub
Published in
4 min readMay 17, 2017

--

Kotlin is a statically typed language which means that it does most the check at compile time rather being dependent on runtime. Well this language has stayed in the development period for quite 6 years and when it was released it was production ready right form the beginning. It’s quite similar to Scala language and it also inturn requires jvm to run hence it’s completely interoperable with Java.

What makes kotlin a better choice?
Well kotlin has overcome the issue that programmer faces with java like the greatest issue is null pointer exception which this language claims that it will never produce null pointer exception until it’s a fault of the programmer how do they do that we will see it later.

Features of kotlin:

  • Extension function
  • High order function
  • Inter-operable with java
  • Null safety
  • Smart cast
  • Default and named arguments
  • Multi-value return from function
  • Data class

We will see the examples for all those features regarding how to use them but what is important to know here is how to use kotlin as the syntax varies a lot as compared to java. For all those programmers who have learned Scala it will be a left hand play for them as it closely resembles with it.

Get started with kotlin
Just like program execution starts with public static void main(…) in java same way in kotlin it start with fun main(…). The syntax that it follows is variableName:DataType
like for example to declare the variable of integer type the
java syntax is: int name;
kotlin
syntax is: var name:Int=10; //in case the value is mutable
kotlin syntax is: val name:Int=10; //in case value is immutable
Now as you can see that I initialize the variable at the time of declaration itself so that kind of scenarios it’s permissible to omit :Int and directly write var name= 10; based on the type of value it will automatically assign the data type to the variable. Following is the example of correct implementation

fun main(args: Array<String>) {
var name:Int=10; // with the data type
var varName = 20; //without mentioning data type
//both the methods are correct so no error here.
println(name);
println(varName);
}

Following is the example of wrong implementation

fun main(args: Array<String>) {
var name:Int=10;
var varName;
varName = 20; // As data type is not defined so it's an error.

println(name);
println(varName);
}

In case you are wondering what does fun means that it’s actually a way to define function just like you do in php by writing function keyword here it’s the short form by using fun keyword.

How to define a function?
Well the syntax of function is

fun functionName(optionalArg....) {
.....
}

fun is the keyword to define a function. Now a special care has to be taken while using parameter with function have a look on example below

fun check(name:String) {
println(name);
}

Now if you have noticed something than it’s the keyword var or val which is missing here now that is as intended the var and val in function parameter is not allowed. Following is the wrong implementation as var is used

fun check(var name:String) {
println(name);
}

Create class in kotlin
To create class just use class keyword it’s exactly same way as it’s in java but the similarity end there itself.

class Employee{
}

Now if you have only one constructor than that constructor is called as primary constructor and that is declared with the name of class itself.

class Employee constructor(id: Int, name:String) {
}

In case constructor does not have any annotation or visibility than the keyword constructor can be omitted.

class Employee(id: Int, name:String) {
}

If constructor has to be initialize than that has to be in the block of init

class Employee(id:Int, name: String) {
init {
println(name);
}
}

Note that all the class by default are not inheritable if you want to inherit a class than define that class as open.

open class Base {
.....
}
class Derived() : Base(){
.....
}

The way to use inheritance is quite similar to the c++ class inheritance.

Well there are quite a long list of features for classes but this article mainly focus on familiarize with kotlin so let’s see the new feature which will make you to think why haven’t used kotlin till now.
The model classes are often created with the boilerplate of getters and setters but as you use kotlin than forget about those boilerplate rather your model class is now just a one line of code that’s it. Have a look below on how it’s done

Using java

public class Employee {
private int id;
private String name;
private String address;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}

Same model class created using kotlin

data class Employee(val id:Int, val name:String);

Adding data in front of class will automatically create it as a model class so to access the members just call object.name_of_variable for example.

fun main(args: Array<String>) {
val emp = Employee(10,"name");
println(emp.id);
}

Note here that no new keyword is needed directly use the class name to declare a variable. Also if the variable are mutable than use var instead of val.

That’s it for the glimpse of kotlin a single article will make it too long to be read so wait for the next article to see about the remaining features and how to use them.

See the following article to read about kotlin best features

--

--

Pankaj Rai 🇮🇳
AndroidPub

Software Engineer | GDE Android & Firebase | YouTuber — All Techies