Kotlin or Stick to the Good ‘Ol Java?

  • 2019-04-11 02:08 AM
  • 47

The hullabaloo about Kotlin and the myriad reasons to ditch Java and learn it has reverberated far and wide across the globe. Several reasons have been opined and penned down in the same fashion…am not going to kiss the blarney stone for this ; just to belabor on the obvious.

Instead, am gonna use a very practical approach to demonstrate to the “doubting Thomases” or naysayers or to seasoned “Javanistas” (like myself), that its time to stop being plastic, pack our bag and baggage, abandon the old ship and aboard the new wave.

For instance, if we want to create a simple program that checks whether a given figure is a square or otherwise, you can do that in fewer lines of code in Kotlin in comparison to java. Still doubting? Check this out…

java
First we will do the obvious, which is create a java class and define our variables as shown in the figure above. We will need two variables to enable the comparison to check if the figure we are presented with is a square or not.

In java, we need to initialize the objects of a class so as to access it.

java
As you can see from the above code, we have initialized our two objects inside a constructor.

The reason for using getters and setters in java instead of making your members public is that it makes it possible to change the implementation without changing the interface. … Getter and Setter methods are used when you want to get or set a field (int, String, etc) which is private in a class, from outside the class. In the code below, we add getters to our class, we omitted using setters in this demonstration because we don’t need it.

java
Up to this point, a kotlin programmer would already have achieved what he wanted, but heck, we have to continue.

A java method is a collection of statements that perform some specific task and return results to the caller. A method can perform some specific task without returning anything.

We will use a simple method that houses an if statement to do a quick comparison for us and check if what we want is achieved or not.

java

The logic here is pretty basic. We all are aware that a square has equal sides, and a rectangle doesn’t, it therefore follows that we will check if our given width equals length. If both sides are equal, we have us a square, else its a figure we don’t care the shape.

So we have the class, constructor, getters and finally the engine of our program which is the isFigureSquare() method. We are now set to call our method and invoke the getter to get us our result which is either true or false.

java

Messy? Or just too many lines? Be the judge. If we run this code we get our result as shown below.

java
The same logic can be implemented in Kotlin using two very unique ways. Or rather am gonna show only two ways to do it, if you have a better way, please let me know.

As is the norm, am using IntelliJ as my default IDE for this project. We will start with creating our class.

java

Note that the modifier public disappeared when writing code in
Kotlin. In Kotlin, public is the default visibility, so you can omit it.

java
Notice how quick we did this in Kotlin? Just by creating an Object of our Class, we easily got access to the class and printed the result out? Now if this is not abracadabra to you, i mean the conciseness, then you must be too old!

In my next method, am going to show you how to write a custom implementation of a property accessor in kotlin. This even makes our code beautiful, concise and just sexy…haha, if that word can be used in this context.

java
First we declare a class CheckFigure that can check whether it’s a square or not. You don’t need to store that information as a separate field, because you can check whether the height is equal to the width on the go.

java

The property isSquared doesn’t need a field to store its value. It only has a custom getter with the implementation provided. The value is computed every time the property is accessed.

When you invoke the property as shown above and run the code we get the same results. Try it out and be the judge! Which method did you like?

java

In conclusion,the search for a tenable Java alternative reached fruition with Kotlin, a statically-typed programming language running on the Java Virtual Machine (JVM). Essentially, Kotlin stripped Java of its complexities and came up with a refined alternative that is as versatile as its predecessor. Try it out, you might just fall in love, again….

Originally published by WadeQ at https://blog.usejournal.com/kotlin-or-stick-to-the-good-ol-java-a2ce0ba684a1

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

React Native Vs. Native App Development: What to Choose in 2019
How to make your Kotlin Android animations accessible
Top 5 Kotlin Features That Every Android App Developer Must Know
Kotlin Microservices With Micronaut, Spring Cloud, and JPA
Kotlin for Beginners: Next Android platform language
The Complete Android Kotlin Developer Course
Kotlin for Android: Beginner to Advanced
Kotlin for Beginners: Learn Programming With Kotlin
Android Kotlin Development Masterclass using Android Oreo
Kotlin for Android & Java Developers: Clean Code on Android
Kotlin Android Development Masterclass - With Android Oreo

Suggest