More Information
- Code for this Koan can be found here.
- This is the 2nd in a multipart series.
If you want to read more, see our series index
The second Koan is meant as an exercise of the IDE and is targetted at those moving from Java, as such this was a mixed bag for me.
The goal is to get a Kotlin version of Java code by letting the IDE do the conversion, but I decided to tackle it myself.
Initially, I thought I needed to do the entire file and not just the task function. So, what does the entire code look like when changed?
Java
|
Kotlin
|
The first thing you'll notice is that the java.util.Collection
is gone since Kotlin has its' own implementation and it is imported by default.
Next is the lack of Visibility Modifiers in the code. This is because everything is public by default. In addition to public there are protected, private and internal which work the same as with .NETs modifiers.
The next change is the variables, you don't need to define the type before the variable... you just define val for read-only variables or var for mutable variables. This is similar to C#s var keyword.
The final change is the lack of semicolons. This doesn't mean Kotlin doesn't have them, it means they are optional. You can add them as if it was Java or C# with no issue, it is you don't need them unless you are doing something confusing for the compiler.