My new chapter of Android programming!
Is this a Java framework or considered a language? I’m really not sure since the language is running on JVM. But anyways, this is my other road to Google journey to immerse myself into the world of Google’s mobile development world. To do so, let’s start with Kotlin’s fundamentals!
As you may know, being an Android developer has been my dream since last year. It’s especially because of my morbid love for Google’s products and the OS but also for my familiarity with the platform’s language Java. Since I’ve been coding in Java both professionally and personally for the past couple of years, it’s now my de-facto go-to language whenever I code, for example building an Android app.
However, all thanks to Google-vs-Oracle’s ugly drama, Java became a second-class citizen in Android’s developing world, and Kotlin, co-developed by Jet-Brain and Google, is now the first-class citizen when it comes to Android programming.
What’s great about the language is that it runs on JVM and the linguistic mechanism is quite similar to that of Java, more specifically it’s considered a Java’s framework, so if you’re already familiar with Java, you can learn probably master language without any problems (I guess).
So, along with many other coding series I’ve been working on, I’ll keep track of my Kotlin-learning journey here as well with this new series: Android Programming.
For a starter, I’ll go through the very basics of Kotlin, but one crucial requirement for the readers is that you may need to know because of Java programming (or programming experience in any language). Otherwise, you may experience some trouble fully understanding the context of it.
Anyways, let’s dive into it!!
Run on Linux server
Whatever environment you want to work in, you don’t have to follow my way of coding. In my environment, I run Kotlin programs on my home Linux server.
And here’s the Kotlin installation process (It’s easy, don’t worry).
First things first, you have to confirm Java is already installed on your machine. Check it with the following command:
java --version
Once it’s confirmed, install Kotlin with the following command:
sudo snap install --classic kotlin
Once installed, open a new text file with whatever text editor you prefer. In my case, I opened it in vim. Here, I just wrote a very simple hello-world code: hello.kt.
Once finished writing the code, compile the text file.
kotlinc hello.kt -include-runtime -d hello.jar
If it successfully complied it, run the code:
java -jar hello.jar
Here we go, it successfully runs the code, yeahhhhhhhhh!!!
Kotlin fundamentals: variable types
From this point on, let’s have our focus on Kotlin fundamentals.
Int | Holds 32 bits |
Byte | Holds 8 bits |
Short | Holds 16 bits |
Long | Holds 64 bits |
Float | Holds 32 bits |
Double | Holds 64 bits |
Some of Kotlin’s features
A number is recognized as Int by default without specifying the variable type/
val num = 1
When you add L at the end of a number, it’s recognized as Long.
val num1 = 100L
When you type a decimal number, it’s recognized as Double by default.
val pi = 3.14
When you add f at the end of a number, it’s recognized as float.
val pi = 3.14f
Here’s a sample Kotlin code that calculates a circle ratio:
Image 02’s the executed result. By the way, as I mentioned earlier, I’m running the program on my home Linux server. The code I’ve shown in image 01 is written on my Windows machine’s Visual Studio Code that is accessing the server.
This has nothing to do with Kotlin programming, but in real-world development, this is a standard way of coding. Not sure about Kotlin, but at least in my workplace, we code in our local Windows machine, and run the code on our server machine. So, if you don’t have any actual experience, it might be quite helpful if you could build your own coding environment at your home with your Linux machine or maybe install it on VirtualBox.
Anyways, see you in the next post!