Kotlin logo

Kotlin

A concise multiplatform language developed by JetBrains

Getting Started With Kotlin for Java Developers

Guest post by Urs Peter, Senior Software Engineer and JetBrains-certified Kotlin Trainer. For readers who’d like a more structured way to build Kotlin skills, Urs also leads the Kotlin Upskill Program at Xebia Academy.

This post opens The Ultimate Guide to Successfully Adopting Kotlin in a Java-Dominated Environment, a series that follows how Kotlin adoption grows among real teams, from a single developer’s curiosity to company-wide transformation.


You don’t flip a switch to ‘go Kotlin’.

You de-risk, you measure, you celebrate the wins,

and only then do you double down.

Adopting Kotlin in an established Java environment isn’t just a technical decision – it’s a journey that requires careful planning, strategic thinking, and most importantly, winning the hearts and minds of your peers. 

After training over 1,000 developers and helping numerous organizations successfully transition to Kotlin, I’ve seen what works and what doesn’t. This guide will walk you through successful recipes for adopting Kotlin, which I have collected over time, from your first experimental playground to large-scale organizational transformation.

This is the journey this series will take you through:

It Always Starts With a Spark, Initiated by You!

It Always Starts With a Spark, Initiated by You!

Why take the effort to switch to Kotlin? Why not just stick with Java and move on? 

The answer depends on numerous factors. While the data clearly shows Kotlin’s advantages across multiple domains, the decision isn’t purely technical. Subjectivity (“I like my language because I like it”) and skepticism towards something new, which is generally a good thing, play an important role. 

However, the evolution of programming languages shows that our preferences and needs change over time. Crucially, each new generation of languages incorporates fresh paradigms – (null)-safety, concise and light-weight syntax, functions as first class citizens, rich standard library, async concurrency, multiplatform support, generative-AI friendly, etc. – that give developers and organizations a decisive advantage. 

Without this natural progression, we’d still be coding everything in COBOL or another archaic language, unable to meet today’s demands. Evolution is therefore not optional; it’s built into the history of our industry.

For this evolution to take root inside a company, though, it takes more than technical merit. It requires enablers – people willing to explore, advocate, and show the value of these new paradigms in practice. In my experience, three types of engineers typically become these catalysts for Kotlin adoption:

  1. The pragmatic, productivity-focused Java Engineer: Experienced developers who see Java as a tool, not a religion. They’re always looking for better ways to get the job done faster.
  2. The quality-minded, modern language enthusiast: Engineers who prioritize readable, concise, and maintainable code. These are often the same people who would have moved to Scala in the past.
  3. Junior Developers: Juniors who ask the simple but powerful question: “Why should I use Java if I can use Kotlin?” Without the baggage of years of Java experience, Kotlin is often a no-brainer for them.

To which group do you belong? 

These early adopters ignite the first stage. But how do you start? Read on… ;-) 

The Play Around Stage: Start Small – With Tests

You’ve heard about Kotlin and want to give it a try without committing to it immediately. 

So the first thing you need is a developer tool where your very first Kotlin seeds can be planted. Here are some options:

  • https://play.kotlinlang.org/ is a great online playground, simply type and run Kotlin code not only on the JVM but also for various other platforms (JS, WASM, etc.).Kotlin Playground
  • Kotlin Notebook is a powerful feature for IntelliJ IDEA that allows you to easily import dependencies, execute code, and even work with data, draw graphs, etc. Here is an example that shows how easy it is to do a REST call with Spring’s RestClient:Kotlin Notebook
  • IntelliJ IDEA has first-class support for Kotlin. This is no surprise, since JetBrains is the maker of Kotlin, and a large portion of IntelliJ IDEA is written in it. So, to start using Kotlin in IntelliJ IDEA – even in your existing Java project – is a breeze:
    • For Maven, simply configure the kotlin-maven-plugin and the Kotlin standard library kotlin-stdlib.
    • For Gradle, you configure the kotlin plugin.

…and off you go!

  • And there’s more! JetBrains recently released the Kotlin Language Server, bringing a full-featured Kotlin development experience to other IDEs beyond IntelliJ IDEA – such as VS Code. Check it out: https://github.com/Kotlin/kotlin-lsp 

Now, you can write Kotlin in your favorite development environment. How can you evaluate the language in a real-world context with minimal impact and maximal insights? In the test suite of an existing Java project! 

This safe and realistic approach to experimenting with Kotlin offers several advantages:

  • Low Risk: Tests don’t affect production code.
  • Learning Opportunity: You can explore the language features in a familiar context.
  • Gradual Introduction: Team members can get comfortable with Kotlin syntax without pressure.

Tips

  1. Try Kotest + MockK: to immediately feel the expressiveness of Kotlin’s testing DSLs (Domain-Specific Language), like the feature-rich assertions (shouldHaveSize(...), infix (value shouldBe 1), etc.
  2. Use the concise and powerful Kotlin Collections rather than Java Streams.
  3. Play with various language features like Nullable types, destructuring, immutability (val, data classes), expression constructs (when, try-catch, if else), and many more.

This is what you get:

Java

@Test
void shouldGetAverageRating() {
   when(productRepository.findAll()).thenReturn(products);

   Map<String, Double> ratings = productService.averageRatings();

   assertAll(
           () -> assertThat(ratings).hasSize(4),
           () -> assertEquals(ratings, products
                   .stream()
                   .collect(Collectors.groupingBy(
                           Product::getName,
                           Collectors.flatMapping(
                                   p -> p.getRatings()
                                    .stream()
                                    .mapToDouble(Integer::doubleValue)
                                    .boxed(),   
                              Collectors.averagingDouble(Double::doubleValue)
                           )))
           )
   );
   verify(productRepository).findAll();

}

Kotlin

@Test
fun `should get average rating`() { //descriptive tests using ``
   every { productRepository.findAll() } returns products

   val ratings = productService.averageRatings()

   assertSoftly(ratings) { 		//powerful testing DSLs (Kotest)
       shouldHaveSize(4)
       this shouldBe productRepository.findAll()
           .groupBy { it.name }   			//concise collections
           .mapValues { (_, products) -> 		//destructuring
                  products.flatMap { it.ratings }.average() }
   }
    verify { productRepository.findAll() }
}

Next in the series

Kotlin adoption usually starts with one person exploring and a few tests proving useful. Those first moments of discovery naturally lead to something larger: evaluating Kotlin in a real project.

The next post in this series will describe that stage and explain how to test Kotlin in production environments.

Urs Peter

Urs is a seasoned software engineer, solution architect, conference speaker, and trainer with over 20 years of experience in building resilient, scalable, and mission-critical systems, mostly involving Kotlin and Scala.

Besides his job as a consultant, he is also a passionate trainer and author of a great variety of courses ranging from language courses for Kotlin and Scala to architectural trainings such as Microservices and Event-Driven Architectures.

As a people person by nature, he loves to share knowledge and inspire and get inspired by peers on meetups and conferences. Urs is a JetBrains certified Kotlin trainer.

image description