A Taste Of Kotlin
Let's explore the basics of a Kotlin application, and just a few of the crucial pieces of the language.
Foreword
I am actively learning about Kotlin, and have only been using the language for a couple of weeks while following tutorials. I will try my best to explain these concepts. If you notice something incorrect please point it out and I'll be more than happy to fix it.
With that out of the way, here are the basics of a Kotlin application, a "Hello World!" if you will.
How to get started
Developer Environment
In the tutorials I followed they recommended to install IntelliJ IDEA to work with Kotlin. In particular they had me do the following:
- install the latest JDK from Oracle
- install IntelliJ IDEA Community Edition
- Once IntelliJ is installed check for updates, mainly as a safeguard
Create a Project
- Open IntelliJ
- Select New Project
- Project Options
- Make sure Kotlin is selected on the left
- Name your Project
- Choose to create an Application on the right.
- Choose a build system, I chose IntelliJ
At this point you now have a blank slate to begin writing some code. The project folder structure should contain a src directory that looks like this
src
|--main
| |--kotlin
|
|--test
The kotlin code (.kt files) that we add will go into that kotlin folder.
Kotlin Entry Point
Kotlin applications start their execution in a main() function, so lets create one in a moment. It typically looks something like this, if you want to pass arguments into the program:
fun main(args: Array<String>) {
// Write your wizardry here
}
However, you can also have it even more simple if you don't want any arguments passed in. I'm going to add a main function that does NOT have any arguments.
Let's add a main function now, and then run it in IntelliJ
- Add a new main.kt Kotlin File to the project at /src/main/kotlin/main.kt with this code
fun main() { println("Well done, it ran!") } - Select the green triangle to the left of your main function and choose run MainKt
To know if you're code ran how you expected, you can view the console output in the Run section of the IDE (look at the bottom left of the IntelliJ window). You should see the text you added into the println(...) function.
Variable Types
There are three different kinds of variables you'll frequently see.
- var - variable which can be reassigned
- val - variable which cannot be reassigned
- const val - global constant, which cannot be reassigned
Lets add a few variables as examples. Replace the code of main.kt with this. We're now using var, val, and const val in our code.
fun main() {
var greeting = "Well done"
println(greeting)
greeting = "HEY"
println(greeting)
// String template example
println("$greeting, Listen!")
println(NAVI_QUOTE)
val newGreeting = "Hey Listen!"
// The assignments below will not work
// newGreeting = "nope"
// NAVI_QUOTE = "value"
}
const val NAVI_QUOTE = "Hey! Listen!"
Example of val, it cannot be reassigned and the IDE warns you.
Function Declarations
Functions can be declared in a standard way, or a very compact way.
Standard
fun functionName(param: TYPE): RETURN_TYPE {
// function body
}
Compact
fun functionName(param: TYPE) = singleExpression
New code for main.kt
Replace the content of main.kt with the following to see both types of functions in action. Run the program if you want to see the output :D.
fun main() {
val standard = standardGreeting("standard!", "Listen!")
val compact = compactGreeting("compact!", "Listen!")
println(standard)
println(compact)
}
fun standardGreeting(prefix: String, suffix: String): String {
return "$prefix, $suffix"
}
// No need to declare the return type
// since it returns the single expression it can "infer" the type
fun compactGreeting(prefix: String, suffix: String) = "$prefix, $suffix"
Takeaway for functions
If you are writing a function that can be returned in a single expression, write it in the compact form because you get these benefits
- You don't have to add curly braces
- You don't have to declare the return type
- Less code means less bugs
Conclusion
This was just a small taste of Kotlin. I showed you the basics of
I have really been enjoying Kotlin. I come from a background of JavaScript and PHP, so having a static typing system has been a very different, but welcome change. No more guesswork!