Kotlin or Stick to the Good ‘Ol Java?

Kotlin or Stick to the Good ‘Ol Java?

<em>Originally published by WadeQ at&nbsp;</em><a href="https://blog.usejournal.com" target="_blank"><em>https://blog.usejournal.com</em></a>

Originally published by WadeQ at https://blog.usejournal.com

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.

java method is a collection of statements that perform some specific task and return results to the caller. A methodcan 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….

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

kotlin java android

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Kotlin vs Java | Kotlin or Java For Android Development | Kotlin And Java Difference

Kotlin and Java have showcased the cut-throat competition in the lastest Android Development. There are a lot of ambiguities amongst developers to choose one, from both. This video will help you to understand the strengths and weaknesses of both with a head to head comparison.

Java vs Kotlin for Android Development — 16 Pros and Cons

The technical difference is significant, but if you plan on doing Android development, learn both. Most of the popular libraries are backward compatible with Kotlin, Java code can be used in Kotlin and vice versa, but to solve a problem or simply understand all the nuances of the documentation, you should know Java. Having mastered both programming languages, you will become a sought-after specialist and secure a good future for yourself in developing applications for Android.

Kotlin Coroutines on Android - How to use Coroutines on Android

Coroutines are a Kotlin feature that convert async callbacks for long-running tasks, such as database or network access, into sequential code. This Kotlin Coroutines tutorial will show you how to use coroutines on Android, and how the new androidx-concurrent library makes it easy to use them to get things off the main thread. You'll also learn how the new library helps coroutines work with Architecture Components. This session also covers coroutine patterns, best practices, and even how to test coroutines!