by Tom Helvick
Java has been the dominant player in enterprise software development for many years. Thanks to the security and speed of the Java Virtual Machine, it became the go-to language for all kinds of large-scale applications at companies around the world.
While the Java Virtual Machine isn’t going anywhere, the Java language syntax’s days may be numbered. Several projects are building languages atop the Java Virtual Machine with cleaner syntax than old-school Java.
At the forefront of that cohort is Kotlin, a new Google-endorsed language popular for building web and Android applications. Many companies are choosing Kotlin over standard Java for new projects, and this post explores why.
Kotlin = Cleaner Syntax, Less Boilerplate, Same Bytecode
If you look at code written in Java and then compare it to equivalent code written in Kotlin, you’ll notice a few things:
- The syntax is remarkably similar. For Java developers, Kotlin is easy to learn and intuitive to use.
- One key difference is how variables get declared. Kotlin is a statically-typed language, but it can infer types or you can set them yourself. Ultimately, this means less boilerplate code like Java’s public void or Person person = new Person()
- You’ll notice that Kotlin code is much shorter and cleaner. For example, Kotlin supports data classes with built-in getter and setter methods so you can do Person.name instead of having to implement and use Person.getName()
When I say Kotlin is more concise, I mean much more concise. The Kotlin website estimates that using Kotlin cuts about 40% off the number of lines of code in a program. That reduced syntactical overhead is a huge win for development speed. Java developers spend a lot of time thinking about and debugging Java’s verbose syntax, and Kotlin greatly reduces that burden.
That said, Kotlin’s shorter syntax still compiles to the same bytecode as Java. That means that Kotlin can leverage all the advantages of the Java Virtual Machine without the hassles of Java’s syntax.
Resolves Known Issues with Java
At the same time, Kotlin’s less verbose syntax is more type safe. For example, Kotlin has non-nullable types that will raise a compilation error, leading to reduced occurrences of the dreaded Java NullPointerException.
Null references have been a thorn in the side of every Java developer since the early days. It’s one of the biggest weaknesses of the Java language that null references are not controlled by the type system. Kotlin fixes that.
In addition, functions are objects in Kotlin with their own type, able to be returned out of other functions. This opens Kotlin to functional programming paradigms that Java’s current SAM-conversions can’t account for.
Other features — including smart casting, higher-order functions, extension functions and lambdas with receivers — provide the ability to write expressive code that’s easy for other developers to consume.
Because Kotlin still runs on the JVM, its increased type safety improves the Java ecosystem overall and provides more security and confidence for Kotlin developers.
Biggest Growth Area: Kotlin for Android with Google’s Support
The major reason Kotlin is growing so quickly is Google’s support for using Kotlin when creating Android apps. Since Kotlin compiles to the same bytecode as Java, you could always use Kotlin to build Android apps. However, with Google’s official seal of approval came attention, tooling, open source packages, and integration support for Kotlin code throughout the Android ecosystem. Most notably, Android Studio has a suite for Kotlin tooling and build.
Kotlin is also fully compatible with older versions of the Java Developer Kit, so it can run flawlessly, even on older devices. This interoperability also applies to existing Java libraries. Kotlin can import from existing open source work, giving it instant access to Java’s well-established ecosystem of tools.
Because Kotlin runs on the same bytecode, a Kotlin program is just as performant as a Java one. In fact, for applications using Kotlin’s inline functions, Kotlin may run faster than Java.
Make the Switch Gradually
Kotlin’s interoperability means that you don’t have to rewrite existing Java applications. You can add Kotlin into your code base gradually and it will play nice with your Java code. This is an attractive option for many companies because Kotlin is easier to use, but they don’t have the time/resources to rewrite an entire code base.
It also means that there’s little risk to testing Kotlin out. Try it on a small project, see if your developers like it. If they do, keep expanding Kotlin throughout your code. You’ll see security benefits, increased developer satisfaction, and perhaps improved performance for your application.
Founded in 1991, Intertech delivers software development consulting and IT training to Fortune 500, Government and Leading Technology institutions. Learn more about us. Whether you are a developer interested in working for a company that invests in its employees or a company looking to partner with a team of technology leaders who provide solutions, mentor staff and add true business value, we’d like to meet you.
Originally published at https://www.intertech.com on February 10, 2020.