The news of Kotlin being one of the official Language for Android Programming is all over the Internet, so we at Dcoder decided to give it a try and list some of the cool functions it provides along with an intro to Kotlin with Dcoder, Yes your beloved IDE.
This blog post is inspired from Intro to Kotlin Google I/O 17 by Hadi Hariri. Let’s start with some of the advantages kotlin brings over java.
1. The Data Class: Â
All your life you have been writing model classes in Java , How much patience do you have? I’ll not say much  but show you the kotlin way to do it.
Clean af? Isn’t it? While doing this in java you will be writing class, constructors, getter, setter, toString and what not? Would be more than 50 lines and with kotlin it gets the job done in 1 line of Code.
Still not impressed, read further.
2. No Semi Column?
Whatttttttt?? What would happen to all those semi column programming jokes? Will Kotlin put all those to death?
Yes and No both, haha Kotlin left this decision to programmers whether they want to use semi column at line end or not so that they can have an endless argument like ios vs Android Guys do . 😀
3. Java and Kotlin together?
Yes, Why not? They both runs on jvm and as kotlin in a language from the developers of jetbrains the name behind the famous IDE intellij , on which Android Studio itself is based.They have the power they wrote it in such a way that your app’s modules written in Java can talk to the kotlin part very easily and vice versa is true also. Even you can convert java code to kotlin inside the IDE, this is one of the coolest features for the developers just starting to move to kotlin.
4. Default parameters:
The feature is inspired from c#, where the parameters can have some default value. Why is even default parameters needed?
if some of the parameters of a function have default values defined, you can even ignore giving values to those parameters, at the same time in some cases where required a different value then default you can send the value as parameter.
Notice the sendPayment() method, we don’t need to add the parameter with default value already defined.
5. Named Parameters
Imagine yourself working in TCS/Accenture/Infy or any large organization and maintaining a legacy code, found an old written method with bunch of parameters with boolean type, you don’t even know what will happen by changing one?
Worry not ? named parameter can save you from such a situation, I’ll show how to use named parameters in the above example.
Notice one more thing you can even change the order for named parameters. 😀
6. Easy functions made even more easy
See the function sum, no curly braces , no return type, no return statement just a lovely one liner function 😀
7. Extension functions
The feature is a also inspired from C#, I absolutely love this thing.
With extension function you can add functionality to existing class. Cool?
If it doesn’t seem cool to you, you probably didn’t understand this.
Let’s take an example what would you do if you have to add some functionality to an existing class in java?
You’ll extend it and make another class and write their the required features? Not in Kotlin, you can directly add functions to existing class.
I’ll show you how, the kotlin way. 😀
We will add a function to count percentage of a number, as there is no default function for percantage, we will use BigDecimal class to add this functionality.
8. Infix notation
Any extension function with 1 parameter can be written as infix notation, lets see how. Suppose you need to calculate n percent of the Money object we had, the ticket to give some discount, lets first write an extension function to do this.
Let’s see how can we convert this to infix notation where you can call the function with
7 percentOf ticket
and there is much more to it, I couldn’t write it all in this one article wait for the next part. 🙂
Happy coding with Dcoder, start doing some kotlin programming on Dcoder, now that you have the reason to do so.
🙂
Let us know your thoughts on kotlin and whether you’ll use it in your next project or not in the comment section below.
* Dcoder is a mobile compiler ide where one can code and learn programming and algorithms, we know the value these emerging technologies create hence kotlin was supported in Dcoder a long ago.We would love to make coders use their free time and make it even more productive. That’s kinda motto we at Dcoder have.
Good job
Thanks a lot for these informations
This is gold! Each commits with thousands of inserting and modifying code will be a thing of the past. I definitely will try Kotlin out!
One of the best app in playstore, juz love it…
In this summer holiday I decided to learn JAVA.
Whether I should go for it or switch to Kotlin?
You should go for java for now, it is the base of every other language. After learning java you can adapt any other language very easily.
Yes, thanks