Skip to main content

Kotlin : An introduction

Photo by wu yi

“A language that doesn't affect the way you think about programming is not worth knowing.”
―Alan J. Perlis

World was nice and there was only assembly(“real programmers code in assembly”) but then one day out of blue he(hey! don't look at me like that I too don't know his name) came and said let there be compilers and interpreters(line by line) that will translate languages easy to read(uh! nah not really) and write into assembly to make programming fun again and that gave birth to an era of exotic new programming languages called procedural programming languages. Procedural programming languages were doing fine but after some time lot's of programmer started loosing their objects because of stateless functions which globally caused chaos. Seeing all this made him angry. So he said, “Procedural languages, I command you to treat everything as an ‘Object’ which will have it's own properties(attributes) and behavior(methods)”. They responded by smalltalk(s) while drinking coffee specially imported from an island named Java. But that wasn't the end of story. Long story short we programmers are hard to satisfy (don't trust me? Well u can ask my girlfriend. She lives in 13th dimension) and hence we will keep on inventing new programming languages for generations to come. In this series of blog posts I'll be talking about one of the most modern if not innovative programming language recently launched for one of the most famous OS (Android) app development; yeah! you guessed it, Kotlin.

This series contains a series of four blog posts viz. Kotlin : An introduction (this), Kotlin : Basic constructs, Kotlin : loops, conditions and switches and Kotlin : The functional aspects. In this post, I will talk about motivation behind the language, portability, features and hello world.

But,what is Kotlin?. Well kotlin is programming language designed by team of JetBrains(R) programmer based in Russia. Andrey Breslav was development lead of the project. Kotlin is not new actually first release of it came in 2011. It gain popularity late when Android team decided to use it as main language replacing old age Java 6 for development. Ever since Kotlin became obvious choice for making apps in Android because of it's readability, conciseness and much more.

Features:

Statically typed
Kotlin is statically typed programming language. Meaning that during compile time type of each and every entities should be known to the compiler. However Kotlin has an active type inference system which can detect the expected type automatically i.e most of the time you don't need to give type explicitly.
Object Oriented
Kotlin is pure object oriented programming language(with few exceptions) meaning that it supports creation of classes and interfaces that can extend or implement (both operations use ‘:’ operator) other classes and interfaces respectively. In Kotlin classes are closed by default meaning they cannot be extended (final class in terms of Java) and interfaces are opened by default meaning that they can be implemented by other interfaces as well as other classes.
Functional
Functional programming is new paradigm(style or method of coding) that contains lambda expressions, higher order or first order or top class citizen functions and top level functions. hush!, so many terms? right. Let's understand them one at a time.
  • Lambda Expression: Lambda expression are inspired from lambda calculus (not directly more info). Basically a lambda expression is compressed form of anonymous function that can be passed to other functions(and methods) (for example callbacks for click events in Java and function objects in C++) and hence they help in cutting down the number of lines in code and make it much more readable and easy to debug.

  • Higher order functions or ...: This is one of the most powerful construct of functional programming languages. This construct allows you to assign functions to variables and then you can pass those variables to other functions as well as return functions from other functions.

  • Top level functions: If you have programmed in Java or C# or any other such pure object oriented programming language before then you would already know the fact that even Hello World program in them requires a main function to be enclosed in top level class. In Kotlin , however you can create top level function that is a function that doesn't needs to be enclosed in any class(like C's main function). You can even have files containing just functions and then can call them wherever you want(provided that you import them first).

Java interoperability
Kotlin is fully interoperable with Java meaning code written in Java (classes, interfaces et cetera) can be used in Kotlin easily. With this feature API(Application Programming Interfaces) written in and for Java can be easily integrated in Kotlin code. However you can not directly write Java syntax in Kotlin because that will cause syntax error as Kotlin is not syntactically compatible with Java.
Portability
Kotlin runs anywhere where JVM can be installed. It can also be compiled to native binaries using LLVM (as of now it's still in beta) or Javascript. Thus it supports a wide range of hardware and code requires little or no modification while transferring source from one machine to other.

hush! that was a lot to read right? Anyway now let's write simple program in Kotlin and see glimpse of what it can do. Instructions to install Kotlin on your system can be found here. Go ahead install kotlin and follow instructions to compile and run the following code.

Hello World


Okay, that's all for this one. If you encountered errors while running the program or ran into errors while installing Kotlin or accidentally launched a Nuclear Missile just comment here and I'll try my best to help you out. Comment if you liked it; Follow if you loved it(show some love naa ❣). Have a nice time.

Comments

Popular posts from this blog

Kotlin Coroutines : A Comprehensive Introduction

Photo by Fleur Treurniet on Unsplash “Redesigning your application to run multithreaded on a multicore machine is a little like learning to swim by jumping into the deep end.” —Herb Sutter, chair of the ISO C++ standards committee, Microsoft® In this article What are coroutines? Blocking vs. Non-Blocking Kotlin Coroutines Suspending functions CoroutineScope Coroutine builders Coroutine dispatcher Coroutine start Conclusion What are coroutines? Coroutines have been around for quite a time now. They are in-fact one of the ideas which helped develop multitasking operating systems. A coroutine in trivial most is a subroutine or function generalisation which in non-preemptive environment (operating system or OS in short) can voluntarily yield the CPU time so that other such sub-routines can use it for themselves without losing the results of previous computations and then can conti

Programmer? A Computer Programmer?

“Programming is like sex. One mistake and you have to support it for the rest of your life.” —Michael Sinz Well, to start with, “A programmer is someone who writes computer code, which tells computer what to do? and how to do it?” . Computing machines are dumb, trust me they are. They have enormous processing power but they don’t have hardware to direct this power to any productive use. And thats exactly where programmer kicks in. Programmers however are not the wizard depicted in the movies. They are humans like me and you. Its just that they think much more rationally than average human does. Types of programmers: The System Programmers: First of all a great tribute to these guys. They are the one who deal with software at the lowest level. They have veteran low level languages (languages which closely resemble with the hardware) —Assembly Language, Machine language in their arsenal. A typical system programmer works closely with hardware engineers bec