Will Kotlin replace Java

Will Kotlin Replace Java? (with Pros and Cons)

In Programming Languages by Rune

Kotlin replace Java

Is Java Obsolete? Will Kotlin take over Java? Ever since Kotlin was released, people in the Java community were worried that Java would be outdated in the future. But what exactly is this new programming language?

Kotlin is a statically typed and quite new programming language that runs on Java Virtual Machine (JVM). Its development started in 2010 at JetBrains, however, it was until 2016 that the first stable version was released, (Kotlin v1.0).

The guys at JetBrains are the same ones who develop and manage Android Studio IDE – Google’s official Android developer tool. By the time of the release of Kotlin v1.0, Kotlin was fast gaining momentum around Android developer communities with plugins for most IDEs already being used.

During Google I/O 2017, which is an annual event for developers and consumers, Google announced Kotlin as an officially supported language for Android Development. Before the announcement, Java had been the prevalent language for developing Android applications.

So the question i see all the time;

Will Kotlin replace Java? Is Java dead? No, Java is here to stay since it’s the main open source language for developing enterprise applications across all major industries; has a huge userbase and codebase. Kotlin compiles to byte code which runs on the JVM; it depends on the Java Class Library. Their interoperability implies Java is not going to be replaced. Furthermore, the announcement at Google I/O indicated that Kotlin is being added to the Android ecosystem, and not replacing Java.

It’s true that Kotlin is developing at a fast rate given its support from Google; and has a concise syntax over Java. However, Java has been around for more than 2 decades and it has proven to withstand the test of time.

Java is a general-purpose programming language; it is not confined to machine applications. Being a platform independent language, it runs on any hardware platform or operating system. Java’s versatility allows it to be used for developing server-side applications, desktop applications, browser apps using Java applet, and embedded mobile applications.

According to the TIOBE Index that ranks the popularity of programming languages, Java has ranked top since 2011.

Why was Kotlin created?

When JetBrains decided to develop Kotlin, they had a view that most programming languages did not have the features they were looking for. JetBrains lead Dmitry Jemerov, however, indicated that Scala was close but had a slow compilation time.

Kotlin was designed to compile as quickly as Java, but with a more concise and pragmatic syntax than Java. Just like Java was named after an Indonesian island, Kotlin was also named after Kotlin Island near St. Petersburg. The team at JetBrains hopes that Kotlin will drive the sales and growth of their IntelliJ IDEA.

After the first stable release of Kotlin v1.0 in 2016, Kotlin v1.2 followed in November 2017. Features such as sharing code between JVM and JavaScript were added. Kotlin is intended to be an industrial-strength object-oriented language, with better syntax than Java while remaining fully interoperable with Java code. This means you can write Kotlin code inside java code – allowing applications to migrate gradually from Java to Kotlin.

Pros of Kotlin

  • Concise Syntax; One of the reasons for developing Kotlin was having a language with less boilerplate code. Kotlin has a shorter syntax than Java, allowing developers to develop their applications faster. It takes about 20% less coding than Java. Furthermore, with concise and less code to write, it results in fewer errors hence less tedious to debug.
  • Interoperability with Java; Kotlin and Java classes can exist within the same project. Compatibility is crucial because Kotlin is still in development and there are not as many libraries and frameworks under it. The interoperability allows you to add advanced Java libraries and frameworks into Kotlin projects without having to change the project into Java. Similarly, we can add Kotlin code into an existing Android studio Java project without having to rewrite the whole app in Kotlin.
  • Combines the best of features from procedural and functional programming paradigms; every programming paradigm comes with its strengths and weaknesses. Having a language that can leverage the strength of both paradigms is a plus. Kotlin shines in this sector, allowing you to write functions that satisfy both paradigms.
  • Avoids the null problem; a major problem bugging Java is nullability; that is the absence of a value. We must, thus, employ lots of null pointer exception try-catch phrases to prevent our apps from closing unexpectedly with ‘application not responding’ errors. While I enjoy coding Android apps, nullability bothers me a lot. Thankfully, Kotlin solves this problem by including null in its type system, so we can write code without worrying about a null workaround.
  • Type-safety; Kotlin is type safe, has optional types, every class is a function and every function is a class. All these help with safety check-ups.
  • Switching from Java to Kotlin is easy; while it is not necessary to switch an application’s Java code to Kotlin, it’s extremely easy to convert. In Android Studio, you only need to install the Kotlin plugin, add it to the Gradle plugin and click convert.
  • Maintainability, support, and reliability; Kotlin is built and supported by major players in the tech field; JetBrains makes some of the most intelligent IDEs, and of course Google, the force behind Android. There is greater productivity and satisfaction because you are using tried and true development tools. Before its first stable release in 2016, Kotlin had gone through a series of Alfa and Beta stage tests to be a highly reliable language.

Cons of Kotlin

  • Small Developer community with limited learning resources; if there’s one thing that’s crucial to learning a new language is having a large community to offer support and learning material. Java has been around for decades and has a large developer community plus a lot of learning resources. Finding answers to any question that may arise when coding Java is quite easy by just going to Stack Overflow. Kotlin, being a fairly new language, it is not so easy to find solutions to issues that may arise, hence making the process of app development difficult.
  • Unstable compilation speed; while Kotlin was designed to compile as fast as Java, sometimes it gets significantly slower. However, I’m sure JetBrains is working on a fix.
  • A learning curve is involved; while Kotlin is built with object-oriented programming in mind, just like Java, the two languages are different at their core and it will require one to take some time to learn or train a team.

Which areas of programming are suitable for Kotlin?

  • Kotlin was designed to improve on Java and should do everything that Java does. Being an official language for developing Android apps, its main use cases fit the development of native Android apps.
  • Kotlin also compiles to JavaScript and can create apps that run in a browser with JavaScript enabled. You can write JavaScript code directly in the Intellij IDEA, then use Maven or compile using the command line.
  • Kotlinx.html is a Kotlin templating engine for building HTML in web applications.
  • Kotlin can be used for developing server-side/back-end applications with concise and expressive code. Ktor framework is made available by JetBrains for use in creating web applications in Kotlin using coroutines. Spring java framework uses Kotlin’s features to offer concise APIs.
  • Kotlin can be used for server-side coding with Node.js since it compiles to JavaScript.
  • Kotlin can be used for enterprise application development since it targets the JVM, just like applications coded in plain Java, and is comparable to Java in performance.

Which areas of programming are suitable for Java?

Java has been around for so long, it has application in all areas of programming. There so many codebases and frameworks to suit any coding scenario in Java for the back-end, front-end, mobile, and desktop applications. Additionally, a large community of developers will help you navigate as you code or learn Java as opposed to Kotlin. Java runs on everything: phones, Windows, Mac, Linux, TVs.

Sample Code snippets showing how Kotlin differs from Java

Hello World in Java

class HelloWorldApp { 
  public static void main(String[] args) { 
    System.out.println("Hello World!"); 
  } 
}

Hello World in Kotlin

package hello 
fun main() { 
  println("Hello World") 
}

Declare an Android Activity in Java (One screen of an Android app)

public class MyActivity extends AppCompatActivity { 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 
  } 
}

Declare an Android Activity in Kotlin (One screen of an Android app)

class MyActivity : AppCompatActivity() { 
  override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.main_activity) 
  } 
}

Should a beginner learn Java or Kotlin?

What to learn?
What to learn?

Java is a good language for learning object-oriented programming concepts. Kotlin has similar OOP concepts with improvements. It’s argumentative which language one should start with, so we will go with an opinion.

Google is pushing Kotlin for Android development. Developers who are already familiar with Java are falling in love with Kotlin because of its short syntax, and actually encouraging beginners to start with Kotlin. However, I would rather encourage a beginner to start with Java.

If you just hit Google search, you will find there are so many tutorials for Java; be it blogs or YouTube videos. Furthermore, there is almost every solution for Android development issues on Stack Overflow written in Java.

Since there are not enough learning materials for Kotlin as of now, it will really be frustrating for a beginner trying to learn Kotlin. Even some tutorials like the ones on the Android Developer website still compare Kotlin examples to Java to help drive the point. So, without Java knowledge, it will be confusing.

There are not as many jobs for Kotlin developers as there are for Java Developers since it’s still a growing language. Finally, once you understand Java, adding Kotlin to your skillset will be pretty easy.

Learning Resources for Java and Kotlin

  • Java Tutorial on Tutorials Point; one of the best free sites to learn any programming language. The step by step guides is easier to understand for a complete beginner.
  • Learn Java for Android Development; this tutorial introduces Java for absolute beginners then proceeds to the Android syntax.
  • Kotlinlang.org; the official Kotlin language website. It has everything you need to master Kotlin from basic beginner material.
  • Keddit; This tutorial on medium teaches you Kotlin by developing an Android application. (This is the best way to learn any programming language).
  • Java Tutorial on Udemy; a Java tutorial for complete beginners. It teaches you programming using video tutorials to make the process easier.
  • Android Kotlin tutorial on Udemy; this course teaches you Kotlin development for Android to develop real-world applications. It also uses videos to make the learning process easier.

Final Thoughts

It’s true that the use of Kotlin for Android development is gaining significant popularity thanks to the support from Google and JetBrains. Its interoperability with Java makes it the best choice for already existing Java developers.

Of course, like any programming language, there’s a learning curve to it and Kotlin is still far from being perfect. The good thing is that you can easily use codebases from either Java or Kotlin in one project.

However, the notion that Kotlin is replacing Java, or whether Java is dead for Android development can never be close to the truth. Kotlin compiles into bytecode that executes on top of JVM hence Java is necessary for running Kotlin.

Yet, its concise syntax and the avoidance of boilerplate code like Java, hence reducing the amount of code you will write, makes it a worthy language to learn for any Android developer.