Taking a look at this language, I thought it would be a good idea to show you guys what I’m learning. I’ll go over it from scratch, hopefully this helps both you and me. Note that these are my notes(no pun intended), cheatsheet style. This post is going to go over basics, namely variables. Before you go any further I highly recommend you setup and IDE like Intellij inorder to follow along. You can follow this guide.
Trying out the snippets.
You don’t need to keep creating new files for each code snippet. I use the interpreter. Yup, you read that right. Kotlin has an interpreter for IntelliJ. You can access it by going to Tools > Kotlin > KotlinREPL. You should see a little REPL pop up below in IntelliJ. This is all you require to follow along, so let’s get started.
Kotlin has all the basic math operators. You can check them out. All this is done in the REPL. Once you’re done typing in, use Ctrl + Enter or Command + Enter to execute the code.
Operators but not exactly
Kotlin actually translates the above operator to something much readable under the hood. It’s much readable imo.
Any decent language has variables(except esoteric languages maybe). Kotlin has 2 keywords for declaring variables: val & var. The main difference is that val is used for declaring constants whereas var is used for declaring…(drum roll please) variables. This means I can’t change the value of val at runtime, but I can do so for var. So something like the following would blow up.
So how do we fix this? Simple we use var.
There is a catch however, note that kotlin is not really a dynamic language, but a statically typed language. Wait, wut? But we didn’t declare any types above? Yes you’re right. Kotlin does the magic by doing something known as type inference. It infers the type of the variable from the right hand value. Now, here’s the catch. I can only reassign variables with the same type. That’s why this will blow up…
One cool thing to notice is that, kotling doesn’t enforce the use of semicolons(great for python kinda programmers).
Now that I’ve told you about types, how do we explicitly declare a variable with a type. Kotlin uses a funny syntax(funny to me because of my background in ‘old’ languages). The syntax is kinda like the following
Let’s declare a string type variable and print it.
The other types are Char, Int, Float, Double and a few more which you can find in the kotlin documentation.
I’ve faced this problem a lot, and I think that many of you have faced the same problem too. String concatenation in some languages is just a nightmare. Mostly end up many times with this pile of junk…
Kotlin has the solution to this problem. It deals with this nightmare using template strings. So the above piece of junk becomes more concise and readable.
Null Type Safety
A huge ass thanks to the creators of this language for dealing with this pain in the behind of Java. No more “NullPointerException”. Kotlin just won’t let you write shitty code. Doing something like this would blow up. (what is this? a Micheal Bay movie??).
What if I have those “situations” in which I really really need to use a null type. Kotlin isn’t some control freak bruh. It let’s you do that too. Like so…
Notice the ‘?’ at the end of the type. This tells kotlin that this variable may or may not be null. Kotlin howerver forces me to use ‘?.’ and ‘!!.’ calls on such types.
So that’s that for this post. Moving on we’ll dive deeper into functions and arrays.