So I’ve been coding with Java for the last three years and since my brother really wants me to move away from that I’m willing to take a look at Kotlin.
For my experience I’m starting *if the lecture from my brother doesn’t count* with the “Kotlin Fundamentals” course by Kevin Jones on Pluralsight. So if you’re reading this and have no idea what I ended up writing about… they offer a free trial so check it out.
The first thing that’s probably the most important one is the quote “it’s a better Java than Java” and the course recommends some Java knowledge.
If you are an IntelliJ religious follower user, you already have Kotlin installed right away. Why? Because IntelliJ and Kotlin are both developed by JetBrains so it makes a lot of sense to use them combined. But for everyone else who is too broke to afford this IDE and/or likes Eclipse more, you can download the Kotlin plugin from the Marketplace. If you don’t like any IDE you can install Kotlin from a command line and code in any kind of text editor but… let’s use an IDE. Please.
Let’s compare the main methods: Java goes with public static void main(String[] args) while Kotlin uses fun main(args : Array<String>). There are quite some differences between these two statements and I will make a list for that because I think that it really helps to visualize how different they are.
- The name of the parameters comes after the type in Java and before in Kotlin
- The array in Java is declared with brackets and in Kotlin there is a specific type for it, it’s generic so you can put anything in it
- Java explicitly mentioned the return type, Kotlin doesn’t. In that case, Kotlin sets the return type automatically to Unit.
- Instead of the whole public static void syntax in Java, you will use fun in Kotlin *haha so funny… no? ok.*
So on to the next. How can you now write your “Hello World” to the console? In Java, it’s System.out.println(“Hello World”); in Kotlin, you achieve the same thing with print(“Hello World”). That’s not only a lot shorter, it also doesn’t need the semicolon.
Oh, another really nice thing is, in Java you need to define a class. Every. Time. Kotlin doesn’t force you to do that. If you simply write the method fun main(args : Array<String>) {print(“Hello World”)} it will work. That is a kinda big saving of statements and therefore of time. That works because the compiler is doing the whole “making a class” thing for you. Nice compiler.
But there are some other features which will help you to save lines of code. For example, you don’t need to define getters and setters in Kotlin. Let’s say you want to read and write a name. In Java, you will probably end up with something like this:
private String name;
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
If you want to do the same thing in Kotlin, you only need the following:
var name
Yep. That’s it. You can now access this variable from another file *yes, I specially said file because, you know, you don’t have to specify a class* like there would be a getter and a setter. For example, you can write print(“Hello $name”) and it would print the value of that variable.
By the way, if you want to make your variable read-only use val instead of var.
So I think that’s it for my first blog post. In my opinion Kotlin offers some interesting things that can make your life easier. But I will dig deeper another time and maybe I will let you know here what I’m thinking about it. Hope this post was entertaining and maybe made you curious about Kotlin. If I got something wrong or missed something completely that you think should be included here please let me know.
If you really made it this far, I want to let you know that I appreciated that. Thank you.