DEV Community

Cover image for Part 2: Sneak peek at Kotlin
Clint Paul
Clint Paul

Posted on

Part 2: Sneak peek at Kotlin

This post is the second part of the course,
(https://dev.to/clint22/part-1-why-should-you-learn-android-development-or-programming-in-general-5a8i)

Before we start, think about this. Was it necessary for Google to endorse Kotlin as the first-class language for Android? Was Java incapable of becoming a modern programming language? Or, is Kotlin that Android-friendly? I would say it’s a combination of both. When I started to learn Android development in college ( 2012 ), things were pretty different. As is the case of the programming language, there was no official IDE available for Android. We had to lease “Eclipse IDE” for developing Android apps but, its primary purpose was to build Java applications. So, when Jetbrains created Android Studio on top of the popular IntelliJ IDEA software, everything soon changed for the good.

Jetbrains

Suddenly, Android Developers have an IDE they can call their own. We were not second-class citizens anymore. But, still, Java was used as the primary programming language for Android. Honestly, I was okay with it. I didn’t know I wanted to code in another language until I started coding in Kotlin. Oh, I forgot to mention one thing, it was Jetbrains that developed Kotlin. Google announced Kotlin as the first choice language for Android in Google I/O 2017. I’m still getting goosebumps thinking about that unveiling. For me, it was a breath of fresh air and a chance to master this newly developed language.

Unveiling Kotlin at Google I/O 2017<br>

Why Kotlin is a good option for us Android Developers?
One thing I noticed early about Kotlin is that it is concise. We can reduce a large chunk of code if we write it in Kotlin when comparing with Java. Our codebases were starting to look a bit cleaner. There were Extension functions, Data classes, Lambdas, Primary constructors and Secondary constructors, etc.
They introduced type inference. It was such a blessing. We don’t have to mention the data type of a variable when we declare it. The Kotlin compiler will automatically find out its type. Also, we don’t have to add a semicolon ( ; ) at the end of each line of code like Java.
Eg:
Kotlin with type inference. We don’t have to mention the data type here. Kotlin compiler already assumed that the data type is an integer.

val sum = 10

The same line of code using Java. We have to mention the data type as an integer. If not, the compiler will throw an error.

int sum = 10;

Kotlin is interoperable with Java. That means you don’t have to delete or modify your existing Java classes in an Android project to use Kotlin. You can call Kotlin classes from a Java class and vice-versa. This increased the adoption rate considerably. Apps with millions of users started using Kotlin in no time. Because, they were guaranteed that, it will not interfere with their existing Java classes.
NullPointerException or the billion-dollar mistake.

Java NullPointerException

There is a huge probability that you must have dealt with a null pointer exception at least once in your career. And, chances are, you must have used Java as the programming language. Java is notorious for Null pointer exceptions.
The NullPointerException (NPE) occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable (called dereferencing).
Kotlin is handling this pitfall in the programming world much gracefully. In Kotlin, all variables are considered non-null by default. This means the IDE itself will remind you if you forgot to assign an object to the variable you have just created.
We will look deep into all the above-mentioned features in the upcoming sections. Don’t fret if you couldn’t understand much now. You can always come back here once you deal with all of them during our coding sessions.
Before you go, let’s just code at least a “Hello World.” Then, you can tell your friends that I taught you something and all 😋.

Photo by Tyler Lastovich on Unsplash

Go to https://play.kotlinlang.org/ and replace it with the below code.

fun main() {
println("Hello World")
}

And press the play button.

Kotlin Playground

You can use this compiler if you ever want to try out some basic stuff. You don’t have to install any huge-sized IDEs for that purpose. This is more than enough.
A small exercise for you now. Try to change the code in such a way that it must print your first name and age. You should store the value of name and age in two different variables as well. The result should be something like this,
Clint 27
I know we didn’t learn much about the variables today. But, do some research and see if you can get the result. Always keep this in mind, “All good programmers are exceptionally good at Googling stuff.” If you do find the correct answer, reply with the code in the comments. I’ll be happy to cheer for you. 🥳 If you couldn’t find the answer, no problem. Please comment on that as well, We can correct it together.

Hope you had a great today and we all learned something new and useful. In the next chapter, we will see how to install the JDK and Android Studio. I’m super excited. 😉 Please make sure you are following me on LinkedIn, Medium, GitHub, Twitter, or buy me a coffee.
The article was originally posted at clintpauldev.com

Top comments (0)