Before you get started in Android app development you will need select a programming language you will use to write your Android app.
While Android app development can be done in many different languages including C++, the two most common programming languages in modern native Android app development are Java and Kotlin.
In this article I will take you through the similarities and differences between Java and Kotlin and I will make a case for why as a beginner at Android app development you would want to choose to use either Java or Kotlin.
As an added bonus I have also included some information about cross platform development using frameworks like Flutter and React Native which allow you to create native apps in a single programming language that work across iOS, Android and the web.
What is Java?
Java is a programming language that was created by James Gosling at Sun Microsystems and was released in 1995 which has since been acquired by Oracle during 2009 – 2010.
Java supports class based object oriented programming and is used in a variety of different applications such as web applications, client server applications, APIs and microservices, desktop applications as well as Android apps.
When developing applications in Java the application is compiled into bytecode that is able to run on any Java virtual machine regardless of computer architecture or operating system which enables cross platform development.
Java is consistently ranked as the most popular programming language world wide. Since April 2015 Java has achieved the top spot in the TIOBE Programming Community Index as the world’s most popular programming language.
What is Kotlin?
Kotlin is a programming language that was created by JetBrains who is well known for developing popular tools for software development such as IDE’s like IntelliJ IDEA, PyCharm and PhpStorm as well the continuous integration tool Team City.
Kotlin supports object oriented programming is designed to be interoperable with Java. Kotlin is primarily used for Android app development however it can also be used for server side applications and data science applications.
Kotlin v1.0 was released in February 2016 and has been designed to be concise and remove a lot of the boilerplate that you see in Java. Just like Java, Kotlin is compiled into bytecode which is run on the JVM.
Some of the important features of Kotlin include the following.
- Nullability of data types
- Type inference
- Java interoperality
- String interpolation
- Immutable and mutable properties
- Extension functions
- Lambdas
- High order functions
- Inline functions
- Coroutines
Some key events in the Kotlin timeline include the following.
Date | Event |
---|---|
2010 | JetBrains team starts development on Kotlin |
July 2011 | JetBrains announce that they have been working on Project Kotlin which will introduce a new programming language to run on the JVM |
Feb 2012 | JetBrains open source Project Kotlin |
15th February 2016 | Kotlin v1.0 is released |
May 2017 | At the Google I/O 2017 Conference, Google announced official support for Kotlin for Android development |
28 November 2017 | Kotlin v1.2 was released with support for sharing code between JVM and Javascript platforms feature. |
29th October 2018 | Kotlin v.13 was released with support for coroutines |
7th May 2019 | Google announced that the Android platform will become Kotlin first and recommend new Android apps should be written in Kotlin |
Similarities between Java and Kotlin
Java and Kotlin have a few similarities, please see the following list of similarities between Java and Kotlin.
- Java and Kotlin are both compiled into bytecode
- Java and Kotlin both run on the Java Virtual Machine (JVM)
- Kotlin is interoperable with Java meaning that Kotlin can access and interact with Java classes from a Kotlin class
- Java and Kotlin both support object orientation and functional programming
The Case for Learning Java First as a Beginner Android App Developer
1) More Online Resources Available for Learning Java for Android Development
If you are looking to learn a new programming language as well as how to develop apps for Android you will be relying on good quality online documentation, tutorials, videos, code samples and online communities like Stackoverflow and Reddit to help you learn.
Because Java has been the most popular language to build Android apps for so long you are going to have more resources available to you for Java rather than Kotlin.
At the time of writing when searching for questions tagged with Java and Android on Stackoverflow I get a total of 244,333 questions with 85,629 considered not answered by Stackoverflow which equates to roughly 35% of questions asked unanswered.
This compares to questions questions tagged with Kotlin and Android on Stackoverflow I get a total of 18,609 questions with 7,526 considered not answered by Stackoverflow which equals approximately 40% of questions asked unanswered.
2) Easier and Cheaper to Hire Java Android App Developers if you need to Expand
If you need to grow your development team working on your Android app. There is a larger supply of Java Android app developers compared to those that know Kotlin Android app development. This means they will be easier to hire and you don’t need to pay extra for developers with skills in Kotlin.
3) Java has Faster Compilation Times than Kotlin
Java is general has faster compilation times than Kotlin.
If you want to learn more about this, the Uber Engineering team with help from JetBrains did a series of experiments to test the compilation times between applications in Java and Kotlin and published the results in the blog post below.
4) Due to Popularity there will be Job Opportunities as a Java Developer
As you can see from the TIOBE Programming Community index results, Java the most popular programming language. If you are looking getting into software development as a career, although it depends on where you live but generally lots of job opportunities in Java software development will be in demand even if it is outside of mobile app development.
The Case for Learning Kotlin First as a Beginner Android App Developer
1) Google’s Kotlin First Announcement
Google obviously has a major influence of Android and it’s future, it controls the SDK and other tools and platforms surrounding Android app development. In May 2019, at the Google I/O 2019 Conference Google made an important announcement regarding Kotlin and Android development. I will post the quote in full below.
Android development will become increasingly Kotlin-first. Many new Jetpack APIs and features will be offered first in Kotlin. If you’re starting a new project, you should write it in Kotlin; code written in Kotlin often mean much less code for you–less code to type, test, and maintain. And we’re continuing to invest in tooling, docs, trainings and events to make Kotlin even easier to learn and use. This includes Kotlin/Everywhere, a new, global series of events that we are launching with JetBrains where you can learn more about the language, new Udacity courses, and more.
Source: https://android-developers.googleblog.com/2019/05/google-io-2019-empowering-developers-to-build-experiences-on-Android-Play.html
This is big news for the future of Android app development, if you want to want to be an early adopter of new Jetpack APIs and other Android APIs/features and you can’t afford to wait until they are released and documented for Java, you will want to go with Kotlin.
2) Developer Productivity Less Boilerplate and Less Code
The major selling point with Kotlin is that it allows you to write substantially less code when compared to Java for many different tasks such as creating a class.
Data Classes in Kotlin
See a Java code sample for creating class representing a Pet container two properties and getters for those properties.
Implementing this in Kotlin can be done with only a single line of code. See the Kotlin code sample for creating class representing a Pet container two properties. All properties in Kotlin are private, so getters don’t need to be defined, instead properties on an object can be accessed using the dot notation.
In addition to the class fitting on a single line, just by adding the data keyword to the class definition, this will automatically implement the following functions for you saving you even more coding effort.
- equals()
- hashCode()
- toString()
- copyTo()
To learn more about data classes in Kotlin check out the official documentation at kotlinlang.org.
Learn More About Writing Less Code with Kotlin
To learn more about how Kotlin will help you write less code to achieve the same results obtained in Java, check out the YouTube video below with Andrey Breslav, the Lead Kotlin Designer talk at the Google I/O 18 conference.
3) Coroutines for Handling Network Requests and Long Running Tasks
Threading and concurrency in Android app development is difficult to handle with Java, Kotlin has introduced Coroutines that helps in simplifying this substantially.
Coroutines in Kotlin enable you to write callback based code into sequential code which is easier to write and understand.
Coroutines are similar to jobs that are run on a thread. Kotlin has a “suspend” keyword used for functions in Kotlin that can be called in an Couroutine.
Coroutines can be launched in three different ways.
- IO which is for network requests or file input output
- Default which is for CPU intensive work
- Main which is the UI thread on Android
When you need to interact with your UI to update information on screen the Coroutine can switch the context of the Coroutine into Main to ensure the app doesn’t crash from updating the UI outside of the Android UI thread.
To learn more about Coroutines in Kotlin check out the official documentation. Also see the video below that gives a detailed overview of Coroutines in Android in Kotlin.
4) Null Safety to Avoid Null Pointer Expections
One of the big benefits of Kotlin is null safety, Kotlin lets you define whether variables are nullable by appending a “?” to the data type of the variable.
If you define a variable as nullable, Kotlin will throw a compilation error if you don’t handle within your code what to do with that variable if it is null.
This is different to what happens in Java as null pointer exceptions are thrown at run time rather than at compilation time.
To learn more about null safety in Kotlin check out the official documentation.
5) Knowing Kotlin will help you get a Job as an Android App Developer
If part of your motivation to learn Android app development is to get hired as as a professional Android app developer, it will be critical to know Kotlin given the way the industry is moving.
Some of the companies that are using Kotlin at the moment include the following.
- Evernote
- Gradle
- Uber
- Corda
- Coursera
- Pivotal
- Atlassian
6) Risks around Oracle’s Licensing for Java
Oracle made some changes to the licensing for the usage of Java in 2019. These licensing agreement can get complex, but from what has been reported Oracle will be moving Java SE to a subscription based model.
From what I have read, if you shift to the Open JDK you can still continue to use Java for free without incurring a license fee to Oracle.
For more about this it is best to get your information on licensing directly from the source at Oracle. Make sure you get legal advice on this will impact you or your business.
Bonus Options: Cross Platform Mobile Frameworks like Flutter or React Native
There are other alternatives to Java and Kotlin that can be used for Android app development.
There are a number of cross platform mobile frameworks available on the market that allow you to write you mobile app in code once and will build it into a native app that works both on iOS, Android and on the web.
Two well known cross platform mobile frameworks are Flutter and React Native, I have included some brief information on each of the frameworks below.
I would suggest exploring Flutter or React Native if the mobile app(s) that you wish to create do not have hardware intensive requirements and you want your users to be able the access the mobile app with in their browser in addition to both app stores on iOS and Android.
Flutter
Flutter is an open source software development kit created by Google and released in 2017 that supports developing native apps across Android, iOS, web and desktop from a single code base.
Apps are written in the new programming language Dart which was also developed by Google. Flutter has support for stateful hot reload which allows you to refresh your app without restarting it and loosing state when you update the source code.
To learn more about Flutter check out the online documentation at the link below.
React Native
React Native is an open source mobile app framework created by Facebook and released in 2015 that supports developing native apps across Android, iOS, web and Windows (via UWP).
Apps are written in the programming language in JavaScript. Just like Flutter, React Native also supports hot reloading.
To learn more about React Native check out the online documentation at the link below.
https://facebook.github.io/react-native/docs/getting-started
Conclusion
In general, because of the May 2019 announcement from Google I would recommend beginners at Android app development start with Kotlin however there are some exceptions where I think Java may be better.
There are two scenarios I would recommend learning Java first for Android app development as a beginner.
- If you are building a team of Android app developers to work with you to create your Android app
- If you know a small amount of Java, you are new to Android app development and your project is so time sensitive that you can’t spare the time to learn Kotlin at this point in time
And remember nothing stops you from learning both languages for Android app development at a later stage, which I recommend you do once you have the time. Learning how to use both Kotlin and Java for Android development will help you solidify your knowledge in both languages as you compare and contrast the two.
I would advise not to stress too much about selecting the correct programming language, Java and Kotlin are both great programming languages and once you have learnt your first programming language you will realise how easy it is to pick up a new language.
Read On
If you are looking for resources to learn Kotlin for Android App Development I would recommend checking out the resources page on the official Kotlin website at the link below.
https://kotlinlang.org/docs/reference/
If you are looking for resources to learn Java for Android App Development I would recommend checking out the Build Your First App tutorial available in the developer guide section on the Android Developer website at the link below.