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 Coroutines on Android - How to use Coroutines on 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!

Kotlin Coroutines on Android - How to use Coroutines on Android.

Coroutines are a feature of Kotlin that help convert callback-based code into sequential code, making code easier to read, write, and understand. This session 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!

What's New in Java 19: The end of Kotlin?

What's New in Java 19: The end of Kotlin?

What's New in Java 19: The end of Kotlin? Will Kotlin still have a place in the JVM language ecosystem in a few years? This tutorial will compare and contrast how the two languages approach solving the same problems of today's Java. We'll peer a few years into the future to see where both languages are headed and determine whether we still need a Kotlin

Kotlin's introduction was a breath of fresh air at a time when the pace of innovation in Java felt glacial. Since that time, development of Java has rapidly increased with an emphasis on fixing the pain points of the language and the efficiency of data representation in the VM. Will Kotlin still have a place in the JVM language ecosystem in a few years?

This talk will compare and contrast how the two languages approach solving the same problems of today's Java. We'll look at data-carrying types, asynchronous programming, nullability, and more. For some of these, we'll also see how one language sometimes influenced the other. Finally, we'll peer a few years into the future to see where both languages are headed and determine whether we still need a Kotlin (spoiler: we probably do!).

Kotlin vs Java: Which is the best for Android Developers?

Kotlin vs Java: Which is the best for Android Developers?

In this article will help you spot the major differences between Kotlin vs Java

With the undisputable notoriety of Java as a programming language, Android was found wanting a cutting edge language to compete. Kotlin filled that hole no sooner it was declared as the official language for Android development.

I’ll be discussing the topics in following order:
What is Kotlin? What is its importance?What is Java?Parameters to compare Kotlin and JavaAdvantages of Kotlin over JavaAdvantages of Java over KotlinIs Kotlin going to replace Java?
Let’s begin!

Kotlin vs Java: What is Kotlin? What is its importance?

Kotlin is a statically typed programming language used for Java Virtual Machine (JVM) and JavaScript. The **Kotlin **project was born out of the aspiration for heightened productivity.

Kotlin is officially supported by **Google **for mobile development on Android. Ever since the release of Android Studio 3.0 in 2017, **Kotlin **was included as an alternative to the standard Java compiler.
It is also described as a general-purpose language.Kotlin introduces functional features to support Java interoperability.Helps to improve the coding experience in a way that is both practical and effective. The advantage with **Kotlin **is that you can compile it to a JavaScript and interoperate it with Java.It is supported by leading IDEs and is compatible for **Java **version 6 or 8.
Talking about the importance of this language, it is the most strongly supported **JVM language in the Android ecosystem, aside from Java. **With Kotlin you can write significantly less code when compared with Java. Fewer lines of code imply smaller file sizes for Kotlin, compared to Java equivalents.

Kotlin vs Java: What is Java?

Java is one of the oldest and easiest languages. It has been at the top for more than two decades and it is still a very popular programming language. It is an object-oriented programming language developed by Sun Microsystems, which is currently owned by Oracle.

Java does a whole lot more than just develop Android applications. So if you know your way around Java, you become a part of the bigger Java community and market, hence you have a lot more career opportunities.

This is a reputable programming language. It is mainly:
Concurrent where you can execute many statements instead of sequentially executing it.It is class-based and an object-oriented programming language.Independent programming language which follows the logic of “Write once, Run anywhere” i.e. the compiled code can run on all platforms which supports java.
In simple words, it is a computing platform where one can develop applications.

Now that you’ve got a gist of what is Kotlin and what is *Java, *let’s discuss the parameters to compare Kotlin & Java.

Kotlin vs Java: Parameters to compare Kotlin and Java

I will consider the following parameters to compare Kotlin vs Java.
Compilation timeNull safetyDevelopment speedLambda expressionsCommunity support### Compilation time

Kotlin shows slower compilation speed than Java in most of the cases.

Java’s compilation time is comparatively fast.

Null safety

Kotlin has inbuilt null safety. The not so famous* NullPointerException* is largely responsible for Android development mistakes. Android mainly relies on Null to represent the absence of a value, but it can easily destroy an application. Kotlin easily solves this problem by incorporating inherent null safety. Due to this addition, many developers are saved from writing extra code to work around the issue.

Java does not support Null safety as this was incorporated in recent updates.

Development speed

Kotlin’s code development speed is fast.

Java’s code development speed is comparatively faster.

Lambda expressions

Kotlin supports Lambda expressions which are essentially anonymous functions that can be treated as values. You can pass them as arguments to methods, return them, or do any other thing you would do with a normal object.

Java does not support lambda expressions.

Community support

Kotlin has limited learning resources as it is a small and new community.

Java is a vast programming language so the community circle is bigger than Kotlin.

Kotlin vs Java

Kotlin vs Java: Advantages of Kotlin over Java
Kotlin is interoperable.Concise.Easy learning curve.Provides an enhanced run-time performance.### Interoperability

Kotlin is interoperable with Java, which means that you can leverage all existing Java libraries, JVM, and the frameworks.

Concise

This helps in drastically reducing the amount of boilerplate code in your project.

Easy learning curve

The developers who know Java can easily learn Kotlin. Switching from Java to Kotlin does not have much overhead and adapting to it doesn’t possess any cost.

Provides an enhanced run-time performance

Performance during the run-time is high.

Now let us talk about the disadvantages of Kotlin.

Kotlin vs Java: Advantages of Java over Kotlin

In Kotlin, there is definitely a steep learning curve with Kotlin. Its highly concise syntax, while a great advantage, does require some learning up front.Kotlin shows a slower compilation speed than Java in most cases, even though it does beat Java in a few instances.The Kotlin community is still young and the learning resources are limited, so finding answers to problems can be a little difficult. However, with its growing popularity, the resources and community will expand in time.Since Kotlin is still new, finding experienced developers who can act as mentors for your team can be a little difficult. Everyone out there is still just learning and experiencing it.Some features of Android Studio like auto-complete and compilation tend to run slower in Kotlin in comparison to Java.## Kotlin vs Java: Is Kotlin going to replace Java?

Now, I’ll answer your most frequently asked question, is Kotlin going to replace Java? The answer is no. Kotlin has powerful features and so does Java. They both bind together to treat us with more improvised versions. So, yes, Java and Kotlin go hand in hand for android development.

This brings us to the end of this article where we have discussed the major differences between Kotlin and Java. Hope you are clear with all that has been shared with you in this tutorial.