For programmer's wanting to get to know Kotlin, here is a side-by-side comparison of Java and Kotlin covering features, variables, functions, classes, collections and more.

As to why you would want to get acquainted with Kotlin, there are several reasons. First unlike Java, it's modern and as such does not carry the burden of the past in trying to stay compatible with it at any cost.

However, Java is still going strong as we've seen in "How is Java Doing?-State of Java in 2019" despite being heralded as the 'old horse' of the programming language collective:

Java has always been the favorite - enterprises talked and still talk Java. Look no further than the Fortune 500 list of companies reliance on it. Yes, Blue Chips love it.

But for young startups, those fresh to the art of programming or on the other side, those experienced but looking for better alternatives or feeling adventurous, Java is unfortunately not recommended.

Of course, let's not forget those who deal with Android, the platform that spearheads Kotlin's adoption. This is something pretty evident when prime educators like Udacity move on from Java to Kotlin in their Android Development curriculum. We've largely documented the Java way of doing things on Android in our in-depth 7-part Insider's Guide To Udacity Android Developer Nanodegree, but while this credential is still available its reliance on Java has been quietly, but essentially, superseded. See my review of Udacity's Developing Android Apps with Kotlin in which I stated:

At this point I want to note that "Developing Android Apps with Kotlin" contradicts the essence of the "Android Developer Nanodgree" due to its relation to Jetpack and the new way of doing things, which render the material taught in the Nanodegree obsolete.

In fact it came to me as a shock, having just recently completed the Java-based Nanodegree, to find out how much things have changed; not the philosophy and basic theory which remain as is, but the components which you are now supposed to use in building apps.

It's like a new technology has totally superseded its predecessor, with a degree of churn and rate of change we have repeatedly commented on with new releases of Android studio. Saying that, it's true that the Nanodegree has made an update in incorporating Room, but IMHO that isn't enough.

However, reflecting our recent report, How is Java Doing?-State of Java in 2019, Kotlin adoption appears to be very sluggish:

First, what's somewhat surprising and interesting here is that 62.6% of developers are only using Java, exactly like last year (62.8%). I was definitely expecting this number to go down, given the strong adoption of Kotlin, but it looks like it hasn't, yet.That being said, Kotlin did still grow from 13% last year to 16.5% today.

which means that Kotlin, despite being a perfectly viable general purpose language even suitable in backend environments, still isn't accepted outside Android's boundaries.

Still slow adoption shouldn't be a reason for not trying Kotlin out. If however you need more persuasion then let's get down to the bare bone essentials, that is language-wise; the constructs, the syntax, the features which this Kotlin vs Java comparison helps in deciphering.

Kotlin vs Java hosts a side by side comparison split into several categories, such as Basics, Classes, Collections, Concurrency, Functions etc , providing a birds eye overview of both languages' spectrums.

For example in Basics, we observe how variables get declared under both languages:

Java

var x; // compile-time error

final var xx; // compile-time error

final var y = 1;

Kotlin

val x: Int // compile-time error

val y // compile-time error

val z = 2

or how to set them to null :

Java

final String name = null;

String lastName;

lastName = null;

Kotlin

val name: String? = null

var lastName: String?

lastName = null

var firstName: String

firstName = null // Compilation error!!

Classes and Constructors :

Java

final var file = new File("file.txt");

Kotlin

val file = File("file.txt")

Classes' declaration :

Java

public final class User {

}

Kotlin

class User

Collections:

Java

final var map = Map.of(

1, "One",

2, "Two",

3, "Three"

);

Kotlin

val numbers = listOf(1, 2, 3)

val map = mapOf(1 to "One",

2 to "Two",

3 to "Three")



and so on.

By now I think we would agree that Kotlin is less verbose than Java hence you can get away with writing less lines of code as well as being more succinct .That's especially evident in the Concurrency section:

Java

public CompletableFuture<SomeOtherResult> doBothAsync(){

doSomethingAsync()

.thenAcceptBoth(doSomethingElseAsync()) (

(one, two) -> {

// combine results of both calls here

)}

}



public CompletableFuture<SomeOtherResult> doSequentiallyAsync() {

doSomethingAsync().thenCompose(

something -> {

doSomethingElseAsync(something)

})

}



Kotlin

suspend fun doSequentially() {

val something = doSomething()

doSomethingElse(something)

}

suspend fun doBoth() = coroutineScope {

val deferredOne = async { doSomething() }

val two = doSomethingElse() // while doSomething is working

val one = deferredOne.await()

// combine results of both calls here

}

Let alone that Coroutines is a much greater construct than CompletableFutures.

Being succinct is one thing,incorporating state of the art functionality is another,as Coroutines demonstrated.Other such examples in this category could be:

Extension Functions

Null Safety

Lazy Loading

Operator Overloading

Kotlin also seamlessly interoperates with any given Java library in order to leverage the vast functionality found in Java's ecosystem.

This site is based on the From Java to Kotlin cheatsheet , but has been expanded with much more material.

These listings also serve as a direct translation between Java and Kotlin, acting as a Rosetta Stone, hence very handy if you know the first and want to get acquainted with the second.

Finally, in this path to the wisdom of Kotlin, I would also recommend coupling the cheatsheet with Google Developers Codelab Refactoring to Kotlin where you learn how to convert Java to Kotlin using Kotlin's idioms. Oh,I almost forgot about "Udacity Kotlin BootCamp for Programmers" and a copy of The Programmer's Guide To Kotlin, part of the I Programmer Library either in print or as a print replica Kindle Edition.

I hope that I have persuaded you to at least take a peek at Kotlin's way. If that is so, then just start diving into these guides!

More Information

Kotlin vs Java

Related Articles

The Programmer's Guide To Kotlin

How is Java Doing?-State of Java in 2019

Developing Android Apps with Kotlin

Google Developers Codelab Refactoring to Kotlin

Udacity Kotlin BootCamp for Programmers

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, Facebook or Linkedin.







Comments



Make a Comment or View Existing Comments Using Disqus





or email your comment to: comments@i-programmer.info

<ASIN:1871962544>

<ASIN:1871962536>