Common Java myths and misconceptions
Tags: java, myths
Published: 2020-07-30 12:14:48

---
title: Common Java myths and misconceptions
published: true
description: Debunking several false statements about Java
tags: java, myths
---

There're a lot of incorrect opinions among developers - some root in misunderstanding, lack of knowledge or bad experience from past. Java developers are usually up to date with the changes, but I often hear from non-Java developers and general IT world that:

- Java is slow
- Java is buggy/not secure
- Java is not free, so you need to pay a lot to Oracle for licenses
- Java is verbose, outdated language, with API that is awkward to use

I understand that without proper context, any information can be misinterpreted, so I will try to glue it together so that it makes sense :).

## Is Java slow?

Short answer: **No**.

I remember back at my university days, I was trying to get into competetive programming. It is basically all about algorithms and data structures and you write code to solve some problem. The verification is done on a remote server which tests your solution against input dataset. Only language I knew back then was Java, and the server supported assesment of Java written solutions. The main source of loathing from the guys writing in C was that Java solutions took significantly more time to execute and that's why time limit had to be increased for Java.*

Fast-forward to 2020, **Java is one of the most popular languages in the world** and JVM-based solutions dominate the world of data-intensive computation. Apache Kafka, one of the leading platforms for high-speed stream processing is running on JVM. This fact itself, should disprove the statetment of Java being slow.

When it comes to performance, however, rarely something beats a compiled code. But it is like comparing apples and oranges, since Java bytecode runs on JVM - which gives it one of its main advantages: portability. Once C code is compiled against specific platform, it can be run only on that platform. But Java compiled bytecode can be run on almost any platform.

Other thing is that since its inception, JVM has evolved and there are also multiple implementations which are focusing on increasing the throughput and reduce latency, like [GraalVM](https://www.graalvm.org/).

The _slowness_ usually comes from the experience with working with badly written, outdated applications (e.g. GUI based apps in Swing). Unfortunately, Java gives a lot of freedom to create applications and it might happen that while something works, it does not necessarily need to perform well. I think [this StackOverflow answer](https://stackoverflow.com/a/2163570/1471785) summarizes it the best.

*By the way the slowness was caused by JVM being started so that code can run.

## Is Java buggy or not secure?

Short answer: **No**.

Again, there are many proven examples of Java-based solutions, with high focus on security: payment processing, digital signature, embedded devices (ATMs) and so on. Java has wide support for cryptography, authentication and authorization, SSL etc., so it gives you the tools to build secure applications. However, with any codebase bugs can happen, and JDK is not free of those. Luckily, there're frequent updates which fix the critical issues.

The main source of "bugginess" as perceived by users, other than the application code itself, might come from the fact, that the Java runtime (JRE) is not **up to date** on the machine. If the issue happens with application running on server, it is the fault of infrastructure maintainer for not keeping the JRE updated. And the same is with users. Do you remember these annoying pop-ups telling you that there's new version of Java? Do you remember also closing and ignoring it? Well, you just made your computer vulnerable.

Maybe the issue is with additional component (JRE) that you need to maintain on your computer, but most of the Java applications nowadays are bundled with JRE and do not require users to have one installed. This reduces the issue one degree down.

## Is Java free?

Short answer: **Yes**. 

Where does the confusion come from? In 2019 [Oracle announnced changes in licensing model](https://www.oracle.com/java/technologies/javase/jdk-faqs.html), that require buying subscription if you are using Oracle Java in production. This means that either you have a server based apps that run Java or you have a desktop application that you sell with bundled JRE. Having Oracle Java SE subscription will give you access to extended support.

That being said, if you don't need to have this premium support, you can still opt out and switch to [OpenJDK](https://github.com/openjdk) or any other providers. There are many stable and open-source implementations of JDK, that most of the time will give you the same experience as with Oracle JDK. So the long answer is also **yes**, since you have a choice to select different vendor.

## Java is verbose, "write-only" and obsolete language

Short answer: **No**.

Java has a long history and it was missing some constructs in older versions, but as any language it keeps evolving and improving. Let's take this snippet of code written in Java 7:

```java
    private static void java7() {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Luke Warm", 10));
        persons.add(new Person("John New", 15));
        persons.add(new Person("Elsa Snow", 19));
        persons.add(new Person("Mary Rose", 25));

        Map<Maturity, List<Person>> adultAndChildrenMap = new HashMap<>();
        for (Person person : persons) {
            if (person.age >= 18) {
                if (adultAndChildrenMap.containsKey(Maturity.ADULT)) {
                    adultAndChildrenMap.get(Maturity.ADULT).add(person);
                } else {
                    List<Person> adults = new ArrayList<>();
                    adults.add(person);
                    adultAndChildrenMap.put(Maturity.ADULT, adults);
                }
            } else {
                if (adultAndChildrenMap.containsKey(Maturity.CHILD)) {
                    adultAndChildrenMap.get(Maturity.CHILD).add(person);
                } else {
                    List<Person> children = new ArrayList<>();
                    children.add(person);
                    adultAndChildrenMap.put(Maturity.CHILD, children);
                }
            }
        }
        System.out.println(adultAndChildrenMap);
    }
```

This is unnecessarily long and difficult to read. There've been many improvements since Java 7, so the code above can be now written as:

```java
    private static void java11() {
        var persons = List.of(
                new Person("Luke Warm", 10),
                new Person("John New", 15),
                new Person("Elsa Snow", 19),
                new Person("Mary Rose", 25)
        );

        var adultAndChildrenMap = persons.stream()
                .collect(Collectors.groupingBy(
                        person -> person.age >= 18 ? Maturity.ADULT : Maturity.CHILD
                ));

        System.out.println(adultAndChildrenMap);
    }
```

Current Java release cycle is **6 months** and while version 11 is the LTS (long term support), current version is 14, which brings even more improvements either as preview or stable features. The problem with Java is that it rolls a big "ball of legacy", which needs to support (e.g. [the existence of default methods in interfaces introduced in Java 8](https://stackoverflow.com/a/25784475/1471785)).

Since Java 8, the most notable Java update which was released in 2013, [there have been many updates](https://advancedweb.hu/new-language-features-since-java-8-to-14/) which aim to reduce verbosity and increase readability, including:

- Improved factory methods for Collection API (`Map.of()`, `Set.of()`, etc.)
- Stream API improvements (`takeWhile/dropWhile`)
- Local variable type inference (`var list = new ArrayList<String>();`)
- Improved `String` utility functions (`repeat()`, `isBlank()`, `indent()`, etc.)
- Multiline `String`s:
    ```java
    String html = """
    <html>
        <body>
            <p>Hello, world</p>
        </body>
    </html>
    """;
    ```
- [Switch expressions](https://openjdk.java.net/jeps/325):
    ```java
    int numLetters = switch (day) {
        case MONDAY, FRIDAY, SUNDAY -> 6;
        case TUESDAY                -> 7;
        case THURSDAY, SATURDAY     -> 8;
        case WEDNESDAY              -> 9;
    };
    ```
- [`instanceof` pattern matching](https://openjdk.java.net/jeps/305):
    ```java
    if (obj instanceof String s) {
        // can use s here without explicit casting
    } else {
        // can't use s here
    }
    ```
- [Records](https://openjdk.java.net/jeps/359):
    ```java
    record Point(int x, int y) { }
    ```

As with any language, in order to use it effectively you need to learn all the constructs. For some, the first code example written in Java 7 will be more readable than the second one, but that's just because they have not learned the new ways of writing the code.

## Conclusion

There's a lot to add to each of these topics and it is worth mentioning that sometimes it's not black-and-white situation (especially with the performance). I also know, that similar false statements are out there for other languages (Python being slow, C#/.Net copying Java or JavaScript invented by aliens that are waiting for us to go mad while writing application using it and then taking over the planet).

I hope that you enjoyed this article and feel free to add your most beloved myths about Java or any other language in the comments!