< Hello World ! > Introducing Kotlin

Introduction

Kotlin is a statically-typed programming language that runs on the JVM and also can be compiled to JS source code or uses the LLVM compiler infrastucture.

In 2011, it was introduced by JetBrains team. But, the latest and biggest news came in 2017, when Google, at Google I/O, announced that Kotlin will be the first class support language for Android developement. It does not mean Java is dead but Kotlin works everywhere Java works.

As an iOS developer, I am very excited because its syntax is very much similar to Swift. And also, Kotlin promises - to drastically reduce the amount of Boilerplate code; to avoid entire classes of errors, such as null point exceptions; interportability support to the existing libraries for JVM and Android.

Installation on Mac

Before installaling the Kotlin compiler, we need to check if the JDK is avaliable on the machine or not. We need to install HomeBrew as well.

Why HomeBrew ?


As I personally feel, installing stuff through homebrew makes life much easier and also, it works absolutely fine for me.

Step 1

Check if JDK is avaliable or not. Go to Terminal and type $ java -version .
 
If you don't get any output, then you need to install JDK first . JDK Installation Guide

Step 2

Install HomeBrew. HomeBrew is a Package Manager for OS X, and allows a user to easily install software from the larger body of UNIX and open source software on the Mac. Since it uses Ruby and integrates easily with GitHub, it is extensible by mere mortals, which means the body of packages covered is easy to grow. HomeBrew installation guide.
 
Try command $ brew -v to check if HomeBrew is properly installed or not .
Step 3

Finally, install Kotlin compiler using HomeBrew.
 
Type  $ brew install kotlin command.
 
Once the installation is done, check if Kotlin is properly installed or not.
 
Type $ kotlinc -version and get the version number of currently installed Kotlin language.

Let's write some Kotlin code

Open any of your favorite text editors and type the below code.

  1. fun main(args: Array < String > ) {  
  2.     println("Hello, World! Introducing Kotlin ")  
  3. }  
Save the file as hello.kt .
 
Note - kt is the file extension of Kotlin program.
 
Then, complie the application using Kotlin compiler. Type $ kotlinc hello.kt -include-runtime -d hello.jar .

Before compiling the hello.kt file, make sure your are on the same path.

The -d option indicates what we want the output of the compiler to be called and may be either a directory name for class files or a .jar file name. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. 

After the compilation is done, you must have noticed that the compiler created a .jar file which is nothing but a byte code files of the .kt file that will run on any JVM .

Now, run the applicatio by typeing $ java -jar hello.jar.

Let me know if you face any difficulties in saying "Hi" to Kotlin.

Happy Coding!!
Ebook Download
View all
Learn
View all