Learn it through actual coding!
Kotlin is relatively a new language. Since I’ve been coding in Java for the past couple of years, it’s time to interact with the modern language and catch up with the whole industry’s trend!
As I’ve started coding in Kotlin, there are a lot of new discoveries I’ve found in the language. Given the fact that it is the new generation language that is expected to replace Java at least in the Android app developer world, the Java-compatible language allowed me to interact with modern coding theories and the master philosophy behind it.
So, this time, let’s talk about operations (if-branches and for-loops) and functions. Since I expect my readers to know programming basics, I won’t go through them.
Instead, I’ll show you my interactions with the language. in addition to that, my daily learning process of other languages, such as Java and C, which I’ve been blogging about constantly, gives me extra motivation to learn Kotlin as well.
And as you may know, the coding language of my new coding series, Road to Google, is Java, and I translate my work into C language in another series, Be Excel in C Language. So, you know what, I’ll translate my Java programs into Kotlin as well.
What’s especially amazing about this language is its phenomenal compatibility with Java. And you can actually translate from Java to Kotlin just by copying and pasting it into Android Studio. Did you know that?
Okay, so here’s a Java source code. If you’ve been following my recent blogging activities, you may know that this is the code that solves the container-with_most_water challenge.
To translate it into Kotlin, just copy the entire code, and paste it into Android Studio’s Kotlin source code.
When you paste it, the pop-up messaging windows will come up. Just click “Yes”.
This is the translated version of it. Simply WOW, isn;t it??
Then, here’s what I did.
I pasted it back into my Visual Studio Code and eliminated some unnecessary parts. And as you can see, this is Kotlin’s fundamental operations and functions. If you have some basic programming knowledge, probably you can see how it works, can’t you?
Here are some fundamental differences from Java.
Difference 1: for loop conditions
This is how we write in Java.
for(int i = 0; i < a.length; i++)
And this is how we do in Kotlin. Much shorter and simpler, isn’t it?
for (i in a.indices)
And this is the second loop in Java.
for(int j = i + 1; j < a.length; j++)
Also, this is the second loop in Kotlin. One noticeable and interesting part of this is that Kotlin is much more human-readability-oriented, isn’t it? The part that says “until” is really interesting. This is much more like an English sentence. And I can see the whole industry’s directions from Kotlin – languages are more and more looking a lot like our human languages.
for (j in i + 1 until a.size)
Then, I run it on my home Linux server. here’s the result.
And here’s another version where I inserted some if-branching operations. What needs your attention share is the when-branches, which I never saw in Java. So this one works a lot like switch, isn’t it?
For example, the answer to this program is 28, so this line, “Bingo!”, will be shown in the console. The way it writes is quite similar to that of C’s pointer, isn’t it?
28 -> println("Bingo!")
And the following two lines are in-phrases, which is quite similar to that of SQL queries. Interesting…
in 30..35 -> println("Bigger than 30!")
!in 36..40 -> println("This is out of range!")
And this is the executed result.
One more thing…
As I mentioned above, this is a language that allows me to touch on the latest trend in the whole industry. Just for testing purposes, I inserted the line in 23 that prints out “NULL!” if the answer is null.
And when I try to compile it, Kotlin warned me: “warning: expression under ‘when’ is never equal to null”. As you can guess, it means that the variable answer can never be null considering the given numbers in the array, it warned me to eliminate the unnecessary operation from the source code.
So, what we can see here is that Kotlin tries its best to eliminate the unnecessary and keep the code simple. And this is probably the whole industry’s direction. Through Kotlin, I can learn a lot about my profession.