I am excited for Kotlin, want to know why? because it resembles Swift a lot. So it’s quite fun learning it. So let me start with a small introduction of Kotlin. We’ll go on to the syntax after that.

What is Kotlin?

Kotlin is a programming language which runs on Java Virtual Machine and is based on LLVM compiler infrastructure. Primarily, It was developed by the team of JetBrains developers based in St. Petersburg, Russia. Kotlin is design to interoperate with Java, just as you can do with Swift & Objective-C. Just like Java, The name Kotlin comes from an island situated near St. Petersburg. Recently Google adopted Kotlin as their official language for Android, which brought a sudden fame to it.

Data Types

The basic data types are Double, Float, Long, Int, Byte, Short, Character, Boolean and String. There are also literal constants such as Decimals, Hexadecimals and Binaries. and it has the rest of the Collections same as Java, but here Kotlin has also distinguished between mutable and immutable i.e collections that can be changed after initialization and those that can’t.

Syntax

If you’re coming from the background of Swift or TypeScript, declaring properties, writing methods and classes is quite similar to that.

Declaring Property

Declaring immutable properties

and mutable properties like

Commenting

Writing comments in Kotlin is not different than other languages.

Writing a Functions and Classes

Prototype for writing a function is:

and defining a class goes this way:

Control Flow

For Loops

There are a number of ways you can write for loops, syntax is as follows:

If Statements

If statements can be written similarly as of any other language.

If statements can also be held inside a variable.

When Statements

Its like a Switch in other languages, can be written as follows:

Lambda

In Swift, Lambdas are known as Closure, in other languages they are also called Completion Handlers or Blocks. They can be passed as arguments in functions, and are really helpful while implementing Network classes, writing a lambda function is pretty straight forward. Here is a function which takes in two Integers and return the greater number in the block while the function is being called.

Extensions

It’s one of the useful feature of Kotlin, with this feature you can add further functions and properties to existing classes. Lets understand it by an example. Consider a immutable list.

And consider for some reason our program might access an index out of range from this list, for instance; a 5 or 6 and so on.

Which will leave us with a runtime exception.

What we can do is write an Extension for the List, which will return a null value in case the index is out of range.

 

So, that was it for this tutorail. If you guys have any question please leave it in the comment section below.

Good Day! 🙂

Share:

administrator

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

Leave a Reply

Your email address will not be published. Required fields are marked *