Kotlin’s New Features. K2 & Others.
Table of Contents

Kotlin’s New Features. K2 & Others.

Lately, the KotlinConf 2023 took place and the new Kotlin Compiler 2.0 was announced to go phase alpha. A second major compiler version since the language appeared in 2011. It makes a good opportunity to have a quick peek if it will change the balance in the world of Java Virtual Machine programming languages.

Reminder: What is Kotlin?

If you already know what's Kotlin, feel free to skip this section, and move on to the next one.

Kotlin is an object-oriented, Java Virtual Machine compatible language with a strong support of a functional programming. Because a high compatibility between Kotlin and Java, a first, the main difference between those two languages is the Kotlin native approach to functional programming style and concise syntax, but there’s more.  

The JVM compatible language means that it compiles to the same byte code that Java does, and this allows Kotlin to leverage the full potential of already existing Java libraries. It also means that every application that uses Java can be used by Kotlin and vice versa, Java applications can use Kotlin compiled libraries. It’s a way to start using Kotlin, to add it for new features, and then refactor old code, so the developers doesn’t have to rewrite the whole application from scratch.

The Czech company JetBrains are the creators of the most successful IDE for Java based languages: IntelliJ IDEA. A long time ago, the organization decided to create a language that will be free of all Java language issues, and yet remain compatible (to a degree). Fast forward to today, Kotlin is Open Source, and it’s developed by the Kotlin Foundation, with JetBrains and Google as gold members. Despite the fact, that Kotlin is considered as a young programming language, the cooperation between JetBrains and Google already transformed how Android developers write their code, making it simpler and delivering new functionalities faster, and less error-prone. It also means that there already is a great IDE that natively supports writing applications in Kotlin, making it easy to start to develop in it.

Furthermore, Kotlin supports functional programming paradigm. Functional-style programming allows developers to write more concise, readable, and secure code. Encapsulate logic inside a small functions that can be tested separately. Those functions can be reused, as may be considered as separate entities that perform one task, but makes it efficiently. Combining it with Object-Oriented programming allows writing in more efficient way.

Another difference between Kotlin and Java is type safety. Java object can be null, it means that it may be not initialized with a value. If then we will try to use such object, the error is raised. The program stops whatever it was doing, and the client receives the information about the error. Kotlin changes Java behavior, making a difference between type that will never be null and nullable types. Such small change forces developers to handle potentially dangerous situations (null objects), and as a result they write applications with fewer bugs.

It’s not the syntax improvements made Kotlin so successful, but concurrency - Kotlin has built-in tools for handling concurrency, allowing developers to easily and efficiently write multithreaded applications. Java also supports concurrency, but it’s harder to write efficient, multithreaded code. It’s been a bit easier with Java 8 lambdas. Kotlin enables developers to easily create more efficient applications that take full advantage of modern multicore processors. For example, with the Kotlin Coroutines even one threaded applications can perform multiple tasks, which wasn't the case with Java, where one thread serves one client.

Coroutines preceded the creation of Java similar functionality, the ‘light threads’ in the project loom, by years. Project Loom is available since September 2022, along with the Java 19 release (JEP 425). It’s worth mentioning that many companies are still using Java 8, that's almost 10 years old now. We are now on Java 20.

Furthermore, Kotlin offers many tools and libraries for data processing, such as KoltinX Serialization, Ktor, and Apache Spark. This makes it easier for developers to create applications related to data analysis and processing. In contrast to popular Java libraries, that transforms data from text format to objects in a runtime using reflection, the Kotlin approach is preparing for it in the compile time. Reflection is a programming technique that allows a program to inspect and modify an object's fields at runtime. So every time ex. a JSON will be retrieved by an application, it’s uses reflection to inspect the properties of a Java object and map them to or from JSON keys. This means that developers don't need to manually specify how each property of an object should be mapped to a JSON object - the library can do this automatically using reflection. This approach makes development of application much faster, but application have to do extra work during the runtime. Kotlin fixes this and prepares everything during the compilation.

What is New in Kotlin?

K2 Compiler

Kotlin 2.0 will introduce the new, fully-Kotlin compiler, named "K2". The compiler is the application that translates the human-readable, developers code into a machine language, resulting in a byte code that can be run by Java Virtual Machine (JVM). The resulting byte code is fully compatible with Java code, and can be run on any operating system that JVM supports. This also mean, that you don’t have to change your existing Java infrastructure.

The K2 compiler, written in Kotlin, speeds up the delivery of new releases and features. This is because developers can fully utilize the Kotlin language without being limited by other languages. The high speed of K2 also demonstrates the power and performance of Kotlin applications. In fact, K2 is more than twice as fast as the current compiler version.

There are two main parts of the compilation process. The first, called the "front-end", is when the compiler prepares for compilation. It must parse the source code and check its grammar, similar to how English text has rules. The compiler treats the code as a block of text and checks the language's correctness, syntax, and semantics. Unlike English text, computer languages cannot have syntax errors or ambiguity. The computer must ensure the code follows language rules and constraints. Before K2, this phase took the vast majority of time. That's because Kotlin is a type-safe language, where some types are resolved during compile time. This means developers don't always have to declare them explicitly if it's obvious. However, what is obvious to humans may not be easy for a compiler to figure out, which is one of the reasons why the compilation took as long as it did.

For more details, refer to this video from the "Kotlin by JetBrains" channel.

K2 & Kotlin Multiplatform

The Kotlin multiplatform is a technology that enables developers to share Kotlin compiled code for different platforms. The original idea was to create a business logic once, and then reuse it for both iOS and Android operating systems. Now, with K2 compiler, the code may be compiled to iOS, Android, JavaScript and WebAssembly (WASM).

WASM compiled applications can be run directly into all common browsers, without any additional software nor libraries. All browsers already support this feature including Edge, Chrome, Firefox, Safari and Opera. This promises a new era of browser applications, that have capabilities now reserved only for desktop applications. Google Earth page already is using WASM for rendering the 3D graphics of Earth. It enables powerful Kotlin language features to be used directly within the browser itself.

WASM itself holds unused potential to deliver fully blow feature pages, similar to desktop application directly in a browser. It supports the 3D graphics and because it’s run in an isolated virtual environment within a browser, it is much more secure than conventional web pages. There are many features left to be implemented in WASM, but we believe the community will get there.

Kotlin Domain Specific Language Becoming the Default Build Language in Android Studio

Android developers will now use Kotlin, announced Google. The Gradle build scripts, change their default Domain Specific Language, a subset of the programming language, to offer a simplified scripting capabilities. DSL scripts will replace the default, for now, groovy DSL scripts in a Gradle build script definition.

This shows the benefit of strongly typed Kotlin in contrast to loosely typed groovy. The confidence that Google has in the Kotlin programming language, promises that the Kotlin is a right choice for anyone that wants to build faster, deliver faster and ensure that their code is in a good hands, resulting stable errorless build script definitions.

For now, the Android developers were split between Kotlin language used for development and a Groovy DSL for Gradle Build Scripts. Now, the change will simplify things, with unlocking full potential of Kotlin.

Final Thoughts

Will Kotlin replace Java? We'd say it's on the right path to do so.

Kotlin's functional programming capabilities, are similar to Scala, which in the other hand is considered as difficult language to learn. The strongly typed, mix of object-oriented and functional paradigm, with friendly syntax, makes Kotlin a strong threat to Java. It’s a good compromise between powerful and difficult Scala, and Java's poor functional style capabilities.

Developers thrive to deliver the best, not only a good quality of Java application. The strong support of Google, promises the funding for the open source language that the Kotlin is. The Gradle support, in contrast to Maven's XML based build descriptors, allows developers to use Kotlin for both building and writing the application, lowering the learning curve. Kotlin DSL offers the flexibility of Kotlin language within the build scripts itself that is unknown to maven. With DSL even less experienced developers could adjust the build, without having to look for new plugins.

Lastly, Kotlin already dominated the world of Android Applications, and you will be able to target iOS in the future by sharing essential business logic code (possible now)... or using Compose for iOS (possible in the future).

Now, companies worldwide are starting to recognize the language has unused potential in the web applications world, and could soon eat up Java's market share. With the support of WASM, Kotlin could provide a much better full-stack coding experience, than Java ever had (sorry JSF/JSP).

Consider Kotlin, if you want a modern language that your developers will love. Our developers already do.

Liked the article? subscribe to updates!
360° IT Check is a weekly publication where we bring you the latest and greatest in the world of tech. We cover topics like emerging technologies & frameworks, news about innovative startups, and other topics which affect the world of tech directly or indirectly.

Like what you’re reading? Make sure to subscribe to our weekly newsletter!
Categories:
Share

Join 17,850 tech enthusiasts for your weekly dose of tech news

By filling in the above fields and clicking “Subscribe”, you agree to the processing by ITMAGINATION of your personal data contained in the above form for the purposes of sending you messages in the form of newsletter subscription, in accordance with our Privacy Policy.
Thank you! Your submission has been received!
We will send you at most one email per week with our latest tech news and insights.

In the meantime, feel free to explore this page or our Resources page for eBooks, technical guides, GitHub Demos, and more!
Oops! Something went wrong while submitting the form.

Related articles

Our Partners & Certifications
Microsoft Gold Partner Certification 2021 for ITMAGINATION
ITMAGINATION Google Cloud Partner
AWS Partner Network ITMAGINATION
ISO 9001 ITMAGINATIONISO-IEC 27001:2013 ITMAGINATION
© 2024 ITMAGINATION. All Rights Reserved. Privacy Policy