DEV Community

ivanleomk
ivanleomk

Posted on

Part 1 : Getting Started With Kotlin

Motivation

This article is written for more advanced developers which are looking to get a quick grasp on the Kotlin Language for work or for some interesting projects.

We cover installation, basic syntax and a quick introduction to functions in this article which is Part 1 in a long series.

  • Part 1 : Getting Started with Kotlin
  • Part 2 : Control Flow in Kotlin
  • Part 3 : Loops and Exceptions in Kotlin ( Coming Soon )

Installation

For this specific tutorial, I recommend using VsCode since it has almost everything you will need.

  1. First, install the Kotlin language using Homebrew.
brew update
brew install kotlin
Enter fullscreen mode Exit fullscreen mode
  1. Next, install VsCode Support for Kotlin using the Kotlin extension and Code Runner.

  2. Lastly, create a new file called test.kt with the following code within it.

In Kotlin, we always begin our program with the main function. This function is always named the same and does not take any parameters.

fun main() {
    println("Hello, world!!!")
}
Enter fullscreen mode Exit fullscreen mode

which we can then run by using the code runner extension. If all is good, we should get the output as

[Running] cd <filePath> && kotlinc main.kt -include-runtime -d main.jar && java -jar main.jar
Hello, world!!!

[Done] exited with code=0 in 7.21 seconds
Enter fullscreen mode Exit fullscreen mode

Congratulations! You've successfully installed Kotlin 🎉🎉🎉 !!

Programming With Kotlin

Var vs Val

While there are a variety of nuances to consider when deciding whether to use a Var and a Val, an important consideration is that

A Var is a mutable reference while a Val is an immutable one.

A good rule of thumb is to therefore minimise the use of Vars while sticking to Vals as much as possible.

Types

We use types in Kotlin in order to avoid any potential typing errors which might result in unforeseen problems. We can annotate types in Kotlin by using the command

val one:Int = 1
Enter fullscreen mode Exit fullscreen mode

Kotlin by default gives us the Types of

  • Int
  • Double
  • Boolean
  • String
  • Char
  • String

Which we can annotate as seen above. If we do not annotate the type of a variable, Kotlin will simply infer its type.

Functions

We can also write more complex helper functions which we can call and utilise. Functions take the generic boilerplate version of

fun <nameOfFunction>(parameter_1:type_1,...){
//Function Code Goes Here
}
Enter fullscreen mode Exit fullscreen mode

We can see an example below of a function which multiplies two numbers together

fun multiplyByN(x:Int,y:Int):Int = x * y
Enter fullscreen mode Exit fullscreen mode

Note : Our function takes in two arguments and can be expressed in a single line. As a result, we can write it without using the two curly braces we normally use.

We can also make our function more extensible by using default arguments. Let's rewrite our multiplyByN function with some default arguments.

fun multiplyByN(x:Int,y:Int=4):Int = x * y
Enter fullscreen mode Exit fullscreen mode

As you can see, by adding a default argument of 4, the two function calls below are now identical.

fun main() {
    println(multiplyByN(3))
    println(multiplyByN(3,4))
}

fun multiplyByN(x:Int,y:Int=4):Int = x * y
Enter fullscreen mode Exit fullscreen mode

which we can verify by running it using the CodeRunner extension.

[Running] cd <filePath> && kotlinc main.kt -include-runtime -d main.jar && java -jar main.jar
12
12

[Done] exited with code=0 in 4.687 seconds
Enter fullscreen mode Exit fullscreen mode

Top comments (0)