Site icon AndroidVille

Kotlin vs Java – All that you need to know

kotlin vs android

When you look at the pre-requisites of learning Android Development, chances are that Java would be mentioned as the language you must know. It’s true that most of the Android Apps today use java, but it’s not your only option.

At Google I/O 2017, Kotlin was introduced as an alternative to Java for android development. Kotlin had been stable since 2011, but it was really in 2017 that it took off after Google IO.

You can write android apps in any language that can compile and run on the Java Virtual Machine (JVM).  Kotlin eventually compiles down to Java Bytecode, so Kotlin people get all the language sugar and the benefits of Kotlin and Java people are none the wiser.

But even now that Kotlin is officially supported to write android apps, you might wonder why do you need to make the switch if there is no performance upgrade. And you are right!! There is no performance upgrade if you switch from Java to Kotlin, it will only make your app 800 KB heavier due to its libraries, but just have a look at the code below:

 

This is the Java code for an activity with Floating Action Button (FAB). An on click listener is set on the FAB which when clicked shows a Snackbar.

public class JavaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

Now look at the same code in a Kotlin Activity:

class KotlinActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
    }

}

Kotlin achieves the same thing with considerably less amount of code without any additional overheads. Also, did you notice all the findViewById calls are gone!!

How does Kotlin then reference the Views? It uses the Anko library which statically imports all the views from xml. Isn’t it amazing? But we’ll not talk about anko here as it is a complete tutorial by itself. We’ll stick to our comparison of Java vs Kotlin.

 

Excellent integration with Android Studio IDE

If you are not still using Eclipse IDE ( I hope you don’t), you’d be surprised as to how easy it is to integrate Kotlin into Android Studio. All you have to do is Go To: Tools -> Kotlin -> Configure Kotlin in Project.

It’s that simple!! You don’t have to fiddle around with .gradle files anymore. Android Studio does that for you.

Language is mature and stable

Although Kotlin came into limelight after Google I/O 2017, it has been around since 2011 when JetBrains unveiled it as a functional programming language, an alternative to Scala. Scala had the features of Kotlin and was a functional programming language but had a really slow compile time. This was the motivation behind Kotlin.

And even though Kotlin was unveiled in 2011, its first stable release was completed in 2016, 5 years after its unveiling. Time and efforts were put into it to make the language backwards compatible and to make it compatible with Android.

Kotlin has grown crazy since 2017 and has already been adopted by many famous android developers such as Jake Wharton, Antoinio Leiva and others. It is also becoming prevalent in the Job Description of many startups and companies which are hiring Android developers. Hence it makes sense to switch to it.

Lambdas and Extension Functions

Let’s face it. After the introduction of Lambdas and extensions functions of Kotlin + all of its language sugar, working with Java 6 makes us obsolete.

Kotlin has features like Lambdas, extension functions operations, data classes, sealed classes… You’ll find such things in most of the new programming languages but there is no such thing in Java 6.

Although Java 8 has introduced lambdas, but it still lacks features such as extension functions, data classes etc. And to use Java 8 lambdas you anyway need to include support library so why not give Kotlin a try in the first place.

Null Pointer Exceptions

Oh boy!! Now we’re talking. Null pointer exceptions frustrate all the android developers. Even the inventor of Null has apologized for his mistake and regarded introducing the NULL as his biggest regret!! XD

They can occur anywhere and you only get to know during runtime, when you application blows up in your face.

Although today we have things like annotations or design patterns to avoid NPE but again, they require extra amount of work. But kotlin handles NPE elegantly by:

Lesser Code: Who doesn’t like to write less code. Lesser the code, fewer the errors you commit.

Compile Time errors are great: If you fail at null check at any point in your code, compiler informs you at runtime.

Hence, the language sugar of kotlin provides you a safety net for NPE with lesser code, no annotations and lesser effort. It ultimately translates to increased productivity and less time spent fixing bugs.

It’s really very easy to learn

Kotlin is very easy to learn. For someone already comfortable with Java, can understand kotlin right away, and start rewriting his apps in Kotlin in about 3 days or so.

JetBrains people were very concerned that the switch from java to android must not be very steep. So, as you can see from the code above, the language resembles what you already know.

The only real difference is all the syntax and concepts that are different than java like No Semicolons, different way of declaring constructors, variables, functions etc.

I seriously don’t think that you would require more than a week or so to get used to all the syntax.

 

But its not all green in Kotlin Land

 

It will increase you apk size

To include kotlin language support in you android app, you’d need to include a dependency that’s gonna cost you somewhere around 800KB. While that may not be a significant increase, if your application is already very large, and increase of almost 1 MB may throw your users off.

Lack of community support

There are still many people who do not understand Kotlin completely and would not be able to answer your questions on stack overflow or quora XD

Kotlin for android is still fairly new and hence there is not much community support out there. If you do make the switch, then you might not find all the blog posts, user docs, tutorials if you encounter any problem.

Language sugar is too much

When using Kotlin, it is easy to get lost in all the language sugar and make your code unreadable by anyone else.

Java is more verbose, has more lines of code but is more readable.

Especially the extension functions and lambdas. Just listen to a talk given by Jake Wharton on Kotlin for android developers, and you’d see that it can get really syntaxy to work with and understand.

Java code tends to be easier to read and decipher.

 

Conclusion

Kotlin is here and its being adopted by major companies like Pinterest in their android apps whether you like it or not.

It’s being included in the job requirements of many companies day by day for android development jobs as a mandatory thing and if you don’t keep yourself updated you might be edged by someone who does.

So, if you want to stay relevant and write more elegant code, I’d suggest that you start embracing Kotlin for android and stop fearing all the language sugar it’s got.

You need not start rewriting your apps completely. You can start with rewriting smaller components such as an activity, you pojo classes, recyclerview adapters etc. that don’t do too much. You’ll slowly start getting the hang of it. And as I say, you can only learn by doing, not by reading.

 

Like what you read ? Don’t forget to share this post on FacebookWhatsapp and LinkedIn.

You can follow me on LinkedInQuoraTwitter and Instagram where I answer questions related to Mobile Development, especially Android and Flutter.

Exit mobile version