Kotlin Introduction And Setup IDE



 

What is Kotlin ?

Kotlin is a cross platform statically typed, general-purpose programming language.

Kotlin is designed to interoperability fully with java language.

Kotlin code is more concise in terms of syntax.

Kotlin is an open source programming language.

Kotlin is a combination of functional and object oriented programming features.

Kotlin is widely used for Android application development.

Kotlin mainly targets the JVM (Java Virtual Machine) but also compiles to JavaScript or native code.

Kotlin is sponsored by Jet Brains and Google, through the Kotlin Foundation.

Kotlin was born with the idea of covering those gaps Java leaves and add much more simplicity to the code saving us from writing as much boilerplate code as possible.

Kotlin is sponsored by Google, announced as one of the official languages for Android Development in 2017.


History Of Kotlin :

Kotlin was developed by Jet Brains team. A project was started in 2010 to developed the language for own uses. Kotlin was developed under the Apache 2.0 license.

Kotlin is a programming language introduced by Jet Brains , the official designer of the most intelligent Java IDE named IntelliJ IDEA.

In 2017, Google announced Kotlin is an official language for Android Development.


Features of Kotlin Language :

There are following features of Kotlin language :

      Statically typed : It does not require you to explicitly specify the type of every variable you declare. statically-                                   typed programming language, which makes you able to catch errors at compile-time, as                                         Statically typed programming languages do type checking at compile-time.

      Data Classes : Data class Student(var name : String , var roll : Int) It works as a model class in Java .

      Lightweight : Kotlin library is small compared to others.

      Compatibility : Its compatible with JDK 1.6 and latest.

      Performance : Its same as like java.

      Interoperability : As we know that Kotlin runs on JVM so it is totally interoperable with java. We can easily                                         access the java code from Kotlin application and vice versa.

      Compilation Time : There’s a little overhead on clean builds but it’s way faster with incremental builds.

Smart Cast :  It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
                       If we try to access a null able type of String ( String? = “Hello”) without safe cast it will generate a                           compile error.

        fun main (args : Array<String>){
        var data : String? = "Hello"
        print(data.length)   // compile time error
        }

        fun main (args : Array<String>){
        var data : String? = "Hello"
        if(data != null){
        print(data.length)  // smart cast example
        }
        }

      Functional and Object Oriented : Kotlin provide a rich of built function or methods , lambda expression, lazy                                                              evaluation, operator overloading and many more.

      Null Safe : It provides the safety from most annoying and irritating NullPointerExceptions by supporting                                  null ability as part of its system.

                        Every variable in Kotlin is non-null by default like below :

                        String data = "Hello World"

     If we try to assign null value then it gives compile time error.So,

            data = null   // compile time error

            To assign null value to any string then it should be declared as null able.

      String d? = null  // compile successfully

Note length() function also disabled on the null able strings.

      Reduce Code efforts :It also reduce the code efforts of developer in order to other object oriented                                                             programming language.

      Tools for Kotlin :

There are various platform for Kotlin language where you can develop your Kotlin applications :

1. Eclipse Ide

2. Intellij IDEA Ide

3. Android Studio Ide

 
Eclipse Ide
 
IntelliJ IDEA
 


Android Studio

Following steps helps you to create and run a new Kotlin project in Eclipse IDE.

As we know that Kotlin runs on JVM so we must have JDK 1.8 or above in our machine  and set the path in your system environment variable.

Step 1 : JDK Installation and Setup Path

Step 2 : IDE Installation after installation you need to open eclipse in you machine and go to help menu and select eclipse market place like below :


Step 3 : Search Kotlin plugin and install it like below :


Step 4 : Accept the license and finish it like below :


Step 5 : After finish it will restart IDE so restart the IDE after restart the IDE you need to go File -> New -> Other.. -> Kotlin Project click next like below :

 



      Step 6 : After this you need to provide a Kotlin project name and finish it.  After finish you can see your project is created in eclipse then you need to  click right and create a Kotlin File and then click on next button and after  next button you need to provide a name of file then finish like below :

    



After finish you need to write below code in your file :
    
    fun main(args : Array<String>){
           print("Hello World")
    }

After Now explain the code it is a main method just like Java from where JVM  start application for execution.
    
After In Kotlin programming language method define using fun keyword with    the name of method and that method can be parameterized or                    non-parameterized. Here Array is built class or predefine class that is used  to hold group of Objects.

Like Java main method is used to get runtime arguments from main method type of String here also the same concept and print() method is used to display output on your screen just like System.out.print() or  System.out.println().

Now you can right click on your file and run as a Kotlin and you can get following output on your screen like below :

Output is :

Hello World
    
Following steps helps you to create and run a new Kotlin project in IntelliJ Idea  IDE.

Firstly you need to download intelliJ IDEA and select Community version for JVM and Android Development from below link :

 


After download you need to extract you need to follow step like below :

 
 














 Now you need to start intelliJ Ide from shortcut icon the you get like below screen :


 Now you need to accept the term and condition then exit.

 

 Now you need to select UI theme you can select any one then click on Next Default plugin.

 

 Now you dont need to install any thing from here then you need to click on Start using Intellij IDEA.

 
 

 

 Now you need to select create new project.

 

  Now select Kotlin JVM and then next and give the name of your project then finish.

  
  
   Now right click on src and create new Kotlin File/Class.

   

  Then give the name of file and select file after this you need to write below code and run this file :

         fun main (args : Array<String>){
         print("Hello World")
         }

  No wright click and run this you can get output like below :

  
  

    Now this is the way how to setup Kotlin in IntelliJ IDEA.



   Following steps helps you to create and run a new Kotlin project in                   Android   Studio IDE.


Firstly you need to download Android studio from below link :

https://developer.android.com/studio

After download and install Android Studio you need to open Android Studio then you will like below :

Now select Start a new Android Studio Project then it will look like below :

Now select Empty Activity then click on next button :

Now give the name of your application and select language Kotlin then click on finish button :

After this run this application on your Android Virtual Device or Real Device then you will get output like below :

Now its all about Kotlin IDE setup.

No comments: