Intro To Kotlin
Intro To Kotlin
company that has built world-class IDEs like IntelliJ IDEA, PhpStorm,
PyCharm, ReSharper etc.
It runs on the Java Virtual Machine (JVM), and can also be compiled to
JavaScript and Machine Code.
In this tutorial, I’ll give you a brief overview of Kotlin and its features. I’ll
also help you set up Kotlin in your system and prepare you for future
tutorials.
Why Kotlin?
1. Statically Typed
2. Concise
data class User(val name: String, val email: String, val country: String)
3. Safe
String str = "Hello, World" // Non-null type (can't hold null value)
Since Kotlin knows which variables are nullable and which are not, It
can detect and disallow unsafe calls at compile time itself that would
otherwise result in a NullPointerException at runtime -
However, if you add a null check then the method call is allowed -
if(nullablStr != null) {
println(nullableStr.length())
4. Explicit
Kotlin has a very low learning curve. The basic syntax looks a lot like
Java. If you have a little experience in Java or any other OOP language
then you’ll be able to pick up Kotlin in a matter of hours.
Kotlin is 100% interoperable with Java. You can easily access Java code
from Kotlin and vice versa. You can use Kotlin and Java in the same
project without any problem. This enables easy adoption of Kotlin into
your existing Java projects.
8. Excellent Tooling
Kotlin has excellent tooling support. You can choose any Java IDE -
IntelliJ IDEA, Eclipse, Android Studio. All of them support Kotlin.
Moreover, you can also download Kotlin’s standalone compiler and run
Kotlin code from the command line.
You can use Koltin to build applications for a wide range of platforms
including Server side, Android, Browser, and Desktop.
Kotlin programming language, including the compiler, libraries and all the
tooling is completely free and open source. It is available under Apache
2 license and the complete project is hosted on Github
- https://github.com/JetBrains/kotlin
Setup Kotlin
You can set up and run Kotlin programs in several ways. You can either
install kotlin’s compiler and run Kotlin programs from the command line
or install and setup Kotlin in an IDE like IntelliJ or Eclipse -
Install Kotlin’s Standalone Compiler
$ kotlinc
>>>
Run your first Kotlin program from the command line
Open your favorite editor and create a new file called hello.kt with the
following contents -
println("Hello, World!")
Save the file and type the following commands to compile and run the
program
$ kotlinc hello.kt
$ kotlin HelloKt
Hello, World
Install the latest version of IntelliJ IDEA. Kotlin comes bundled with the
recent versions of IntelliJ. You won’t need to install any plug-in
separately to run Kotlin programs.
Follow these steps to create and run a new Kotlin project in IntelliJ
1. Create a new project by selecting “Create New Project” on the
welcome screen or go to File → New → Project .
Select Kotlin on the left side menu and Kotlin/JVM from the
options on the right side -
1. Specify the project’s name and location, and select a Java version
(1.6+) in the Project SDK. Once all the details are entered,
click Finish to create the project -
3. Now let’s write a simple hello world program in the new file that we
have created. Add the following main() function to
the HelloWorld.kt file -
4. Finally, You can run the program by clicking the Kotlin icon that
appears beside the main() method -
You can also run the program by Right Clicking the HelloWorld.kt file
and selecting Run 'HelloWorldKt' .