Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - BASIC


What is Kotlin?

  1. Kotlin is a High Level Programming Language.

    Which means you don't have to deal with complex Assembly level instructions. You simply need to write the instructions that are quite similar to English Language and simple Mathematics.


  2. Kotlin is a language with a Static Type System.

    Which means Kotlin is a statically typed programming language that is mostly used to develop Android applications.


  3. Compatibility with Java.

    Kotlin is 100% compatible with java. Which means existing java libraries can be used in Kotlin applications.

Why should you learn Kotlin?

  1. Growing Popularity

    Kotlin is becoming popular these days due to its compatibility with java.


  2. Easy to learn

    As said earlier, Kotlin is 100% compatible with java. But when compared with the syntax of Java, Kotlin is easier to understand and write.


  3. Multiple Platfrom Support

    Multiple Platfrom Support

First Kotlin Application


Let us write our first Kotlin Application which just prints 'Hello World' on the screen.


Example :



fun main(){
	println("Hello World") 
}


Output :



 Hello World

Let us dissect the above code and understand in detail:

  1. So, in the above code we are printing 'Hello World' using the below statement,

    println("Hello World")


    We can just remember, 'println()' is the statement used to print something on the screen.
  2. All Kotlin programmes should be written inside the Function called 'main()'. Where, 'fun' is the short form of 'Function'.

    fun main(){
    					
    	...
    }

    Note : We have a separate tutorial on 'Functions'. For now, you do not have to dive more into 'Functions'.
  3. The main() Function is the entry point of a Kotlin application. So, Kotlin executes the code written inside the main() Function :

    fun main(){
    
    	println("Hello World")
    }


    Since, 'println("Hello World")' is inside the main() Function, it got printed.

Moral of the Story


We have to write all our codes inside the main function.



fun main(){

	//OUR CODE SHOULD BE WRITTEN HERE.
}

Where do we write Kotlin related code?


The codes in Kotlin should be written inside a file with extension '.kt'.


So, we will be writing the above code to print 'Hello World' inside a file named 'FirstApplication.kt'.


FirstApplication.kt



fun main(){

	println("Hello World")
}     

How to execute the Kotlin code?


Now, executing the above code is a two step process. At first you need to compile and then run it.


So, open command prompt compile 'FirstApplication.kt':

  1. Compile 'FirstApplication.kt' using the below command

    kotlinc FirstApplication.kt -include-runtime -d FirstApplication.jar
  2. Then run the application using the below command

    java -jar FirstApplication.jar

Output :



 Hello World

Note : Till kotlin version 1.3 the main() function was written using 'fun main()()(args : Array )'.But now it is no longer needed.

Kotlin code till version 1.3



fun main()()(args : Array<String>) {

	println("Hello World")
}    

Kotlin code after version 1.3



fun main(){

	println("Hello World")
}   

Still if you want to write the code using 'fun main()()(args : Array<String>)', it's ok. But as said it is no longer needed.