Java might be the grandaddy of them all, but there are new kids on the block. Let's see what makes Scala and Kotlin great, frustrating, and how they compare to Java.
Since the arrival and maturity of Kotlin, JVM-based languages face quite a challenge, most particularly Scala. You must have heard a lot about Kotlin by now, especially the perks and treats it brings to the Java table. But then there is Scala, a powerful and concise Java alternative. Scala vs Kotlin? Which one is better? Or maybe it's Kotlin vs. Java or Java vs. Scala?
Scala and Kotlin aim to better Java in their own different ways.
To those of you planning to shift from Java to either Kotlin or Scala (can't imagine it the other way around), I am going to paint a "what's what" scenario so you can decide better for yourself.
Let's look at Kotlin first.
Kotlin was developed by JetBrains and reached the market in Feb. 2012 as an open source language. Up until now, Kotlin has had two released versions, with Kotlin 1.2 being the most stable version, released Nov. 28, 2017.
The language grew in popularity on Android because of its perfect compatibility with Java 6, which is mainly the present version of Java on Android. Not just that, Kotlin offers some features that are only in Java 8, and are thus not available to Android developers on Java 6.
In the closet full of 'All that is good about Kotlin,' the two most prominent qualities of Kotlin entail seamless and flawless interoperability with Java. You can call Java code from Kotlin and vice versa without any hiccups or frowns, not to mention the in-built null safety feature. Not seeing a NullPointerException is every Android programmer’s dream.
Android app development is completely transformed into a blissful, non-NPE experience. If you want some detail as to how the null safety works, check it out here.
That is also one of the 'why's' for Google adopting Kotlin on Android as an officially supported language.
Apart from that, take a look at some of the strengths Kotlin has to offer.
Any Java competitor has to be an FP language. Kotlin lacks a little on the higher-kinded types and type safety, but nonetheless, it offers all the features of an OOP style language with an FP approach.
This is my favorite part. Java is great, but the boilerplate code it makes you write is just frustrating and error-prone. Coding in Kotlin is very short, concise, and to the point. Get the job done with fewer lines of code than in Java. The less you have to write, the less often you make errors and expose yourself to bugs. Everyone knows that, right?
One way Kotlin reduces the amount of code is by limiting the use of findViewById.
Kotlin allows views to be accessed from the layout XML, just like you would properties. Instead of using findViewById every time, all you have to do is follow this example.
Although Kotlin is a younger language than Scala, it has the support of two great companies: the formidable Google and the amazing JetBrains, who are responsible for many other IDEs, including the IntelliJ IDE for Android. So there is no doubt about great tooling support there. To all the people who thought of Kotlin as some trend, you could not be more wrong.
Kotlin is already on Android and it is definitely here to stay, grow, and evolve.
But with strength comes some weakness as well.
Pattern matching is not strong in Kotlin. It doesn’t fully support it.
...that would be a no. But it's no frowner. You can still do a pretty fine pattern matching job by intelligently using the when clause. But we all know that intelligence takes a little more effort and skills than your average Joe.
If your applications are already long, then the extra runtime size of 800KB could be a problem for you. You might experience a dwindling number of downloads because of the bigger size of an application.
Nothing is ever all good. With the concise approach Kotlin offers, there is a little problem. With so much going on in so little code, it becomes a little hard to read and understand at first. It might even feel overwhelming to a beginner.
Although Google has joined hands with JetBrains to offer support for Kotlin, it is pivotal to know that Google does not endorse it. Even when you use Kotlin in Android, the autocorrect occurs slower than it does for Java.
As I already mentioned, Kotlin is younger than Scala and, needless to say, Java.
Since Kotlin is younger than Scala, it has fewer libraries, blog posts, and tutorials.
The online support community is quite small. The lack of blog posts, tutorials, and user documentation might become a problem for you, but as trends suggest, this might be filled up soon as more and more people and companies are adopting Kotlin.
Before you make up your mind, let's hop over to Scala.
Scala surfaced to the general public in the year 2004. Developed by Martin Odersky, Scala was released on the Java platform as a general-purpose programming language. The name Scala is not some whimsical notion, it is an abbreviation of "scalable": a language that is scalable according to the needs of its users.
Being released in 2004 sure gave Scala the advantage of maturing more over time and taking the spotlight from Kotlin.
Scala has some common features with Kotlin like conciseness of code, higher-order functions, FP functionality with an OOP style, and interoperability with Java (though not as good as Kotlin). Other than that, here is what Scala is about.
Pattern matching in Scala is no joke. Scala uses the match statement for this purpose, which is a powerful version of Java's switch statement. It allows you to match on any type of data, lists, and even your own types. If you haven't already tried it, I suggest you play around with it a little.
Scala is a little complex, but it has a very flexible code syntax. The proficient use of functional programming features makes the code extensible and hierarchical to the very best level.
Scala has been here for more than a decade. Since it is a powerful Java alternative, it has huge support community. We're talking about YouTube tutorials, blog posts, forums, and let's not forget the official support and user documentation. Anything you find yourself struggling with, you can pretty much find the answer to it.
Scala does not restrict you from overloading operators. You can play with operator-looking functions and define limitlessly. But you need to be careful with this feature. Still, if you can do this right, your code readability will increase. If not, you might end up making your code difficult to understand.
Everything is about speed in the world of coding. A slow program is a big turn-off. When it comes to industry-grade projects, Scala's compile time turns into minutes as compared to Kotlin, which tries to compile as fast as Java, which is in seconds.
Scala is not binary compatible with a few versions. Let's say you compiled with Scala 2.1. The same will not compile with Scala 2.11.
That could be nothing, or it could be your biggest nightmare if you have to work across different versions and update them. Especially if you have production-scale projects.
Scala attempts a good take on managing null safety, but it's just not as effective. The Scala equivalent for null safety is the Option keyword. Using this, there is a considerable chance you might get an NPE at the back door.
Scala and Kotlin are in quite a tug of war. Scala has the edge over Kotlin in some ways, but Kotlin is just as formidable in others. The main differences — where the two languages set themselves apart — is that Kotlin is more like a better version of Java, while Scala is an entirely different kind of Java, so to speak.
While Scala has a bit of a learning curve, it offers unparalleled support for advanced functional programming. Kotlin, on the other hand, is super easy to learn and fun to code in.
But does that mean something is wrong with Java? I mean why change from Java in the first place?
Java is old. Really old. Like, great-grandpa old. And while popular, its popularity actually works against it. Java use is everywhere, which makes it impossible for teams using Java to change their fundamentals, even knowing that Java is facing a lot of technical criticism as well.
No matter the greatness, Java has too much redundancy and lengthy boilerplate code. Kotlin and Scala just happen to be better at what Java was intended for.
Choosing the language primarily depends on the use you need it for. Scala is a good fit for projects with a need for a mix of functional and OOP style programming languages. For places where you need to handle large amounts of data or complex modeling reliant on mathematics, Scala would be best.
However, if you just wanted Java to be less frustrating — but still be Java — then there is nothing better than Kotlin out there. Using Kotlin for Android app development will be the best thing that ever happened to you. Kotlin is like a Java cousin with better looks and who doesn’t talk gibberish. So choose wisely and may the code-Force be with you.