Kotlin or Stick to the Good ‘Ol Java?

WadeQ
5 min readApr 10, 2019

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…

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.

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.

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.

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.

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

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.

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

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.

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.

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?

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….

📝 Read this story later in Journal.

🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: Get the noteworthy newsletter >

--

--