Brief about Kotlin
- Before learning Kotlin, you must have the basic knowledge of any programming language.
Brief about Kotlin
- Kotlin was developed by JetBrains team.
- A project was started in 2010 to develop the language and officially, first released in February 2016.
- Kotlin was developed under the Apache 2.0 license.
- Kotlin is a general-purpose, statically typed, and open-source programming language.
- It runs on JVM and can be used anywhere Java is used today.
- It can be used to develop Android apps, server-side apps and much more.
The following are features of Kotlin:
- Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.
- Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException (null reference) from the code.Interoperable.
- Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin code can be used by Java.
- Smart cast: It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
- Compilation Time: It has better performance and fast compilation time.
- Tool-friendly: Kotlin programs are build using the command line as well as any of Java IDE.
- Extension function: Kotlin supports extension functions and extension properties which means it helps to extend the functionality of classes without touching their code.
Kotlin Environment Setup
- Since Kotlin runs on JVM, it is necessary to install JDK and setup the JDK and JRE path in local system environment variable.
- To setup Kotlin for command line, you have to pre install JDK 1.6+ or above.
- To install JDK and set path of JDK and JRE Path in Java .
Path of JDK and JRE Path in Java
- The path is required to be set for using tools such as javac, java, etc.
- If you are saving the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory.
- However, if you have your Java file outside the JDK/bin folder, it is necessary to set the path of JDK.
There are two ways to set the path in Java.
1) How to set the Temporary Path of JDK in Windows
- Open the command prompt
- Copy the path of the JDK/bin directory
- Write in command prompt: set path=copied_path
- For Example: set path=C:\Program Files\Java\jdk1.6.0_23\bin
- Now your temporary path is set. You can now execute any program of java.
2) How to set Permanent Path of JDK in Windows
- Go to MyComputer properties > advanced tab > environment variables > new tab of user variable > write path in variable name > write path of bin folder in variable value > ok > ok > ok.
- Now your permanent path is set. You can now execute any program of java from any drive.
Install the Kotlin compiler: Manual install
1. Download the latest version (kotlin-compiler-1.8.10.zip) from GitHub Releases.
2. Extract downloaded zip in any of system location (in my case it is in C drive).
3. Copy the path up to bin directory of kotlinc.
4. Open Computer properties and click Environment variables.
5. Click on edit path
6. Past the path of kotlinc bin directory in variable value.
Create and run an application
- Create a simple application in Kotlin that displays "Hello, World!". In your favorite editor, create a new file called hello.kt with the following lines:
fun main() {
println("Hello, World!")
}
- Save the file with name hello.kt, .kt extension is used for Kotlin file.
- Open command prompt and go to directory location where file is stored.
- Compile the application using the Kotlin compiler:
kotlinc hello.kt -include-runtime -d hello.jar
The -d option indicates the output path for generated class files, which may be either a directory or a .jar file. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.
Run the application.
java -jar hello.jar
Run the REPL
You can run the compiler without parameters to have an interactive shell. In this shell, you can type any valid Kotlin code.