Skip to content
Alessandro Autiero
Go back

Some features I'd like to see in feature Java releases

Updated:

Java has been my first programming language and, even though as the years have passed I’ve tried other languages, I can’t help but feel like java does things in a way I really love. Though, I’ve seen a lot of features during these years and thought that those may be good additions, so I decided to just make a list out of them.

Dart’s teachings

Enhanced constructors

Here is how I would create a class to represent a very simple Human in Java 15:

public class Human {
    private final String name;
    private int age;

    public Human(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String name() {
        return name;
    }

    public int age() {
        return age;
    }

    public void cakeDay(){
        age++;
    }
}

Using a record obviously wasn’t an option here as all the fields of a record are final. I also prefer to use accessors instead of getters and setters because most of the new Java APIs are moving towards them(see the new Java HTTP API for example). Now, this looks quite good, but let’s look at the same code in Dart:

class Human {
    final String name;
    int age;

    Human(this.name, this.age);

    void cakeDay(){
        age++;
    }
}

The important part here isn’t that Dart doesn’t use Java/C# style getters/setters or the different access modifier system, but the constructor. This syntax is quite helpful when classes start to get more complex and full of fields.

Named constructors

In java to create a named constructor we usually use a static method that returns an object built using an old-style constructor in the same class. This pattern is commonly used in the JDK, let’s look for example at the Optional class:

public final class Optional<T> {
    private final T value;
    private Optional(T value) {
        this.value = value;
    }

    public static <T> Optional<T> of(T value) {
        return new Optional<>(Objects.requireNonNull(value));
    }
}

Now to reproduce this exact example in Dart we would have to use other Dart specific features that don’t align nicely with the Java philosophy, so let’s create an easier example:

public class Link {
    private final String ref;
    private Link(String ref) {
        this.ref = ref;
    }

    public static Link of(String ref) {
        return new Link(ref);
    }
}

Now let’s see how Dart does it:

class Link {
    final String ref;
    Link.of(this.ref);
}

As you can see also the previously discussed feature is included to make for a really nice syntax in my opinion.

Swift’s teachings

Optional parameters

Java has some really good UI libraries for both Desktop and Web, the best being respectively OpenJavaFX and Vaadin in my opinion. The problem they all have is strictly correlated with Java’s lack of optional parameters in constructors and methods. Let’s look at a very simple implementation of a button in Java:

public class Button {
    private final String name;
    private final String description;
    private final boolean clickable;
    private final int elevation;

    public Button(String name, String description, boolean clickable, int elevation) {
        this.name = name == null ? "Button" : name;
        this.description = description == null ? "" : description;
        this.clickable = clickable;
        this.elevation = elevation;
    }

    public String name() {
        return name;
    }

    public String description() {
        return description;
    }

    public boolean clickable() {
        return clickable;
    }

    public int elevation() {
        return elevation;
    }
}

Now, this is not very convenient: what if I only wanted to specify the name and elevation property? I would need to pass null or a default value for the parameters I don’t care about. Another option is to have the fields mutable instead, have an empty constructor to build the object and use setters to set the properties we care about. This is definitely better, but still a far inferior syntax to the ones that other modern languages such as Kotlin and Swift offer. Let’s now look at how Swift does the same thing:

class Button {
    let name: String
    let description: String
    let clickable: Bool
    let elevation: Int
    init(name: String = "Button", description: String = "", clickable: Bool = true, elevation: Int = 2) {
        self.name = name
        self.description = description
        self.clickable = clickable
        self.elevation = elevation
    }
}

And here’s how we would use it:

Button(
 name: "Mario",
 clickable: false
);

Much better! This is, of course, possible also in methods. The default keyword is already in java, but would probably be too verbose to be used inside a constructor or a method, just an equal operator seems easier, but would be a noticeable inconsistency.

Null safety

Even though the Optional class has been introduced in Java 8, it really isn’t a painless gateway to null safety, let’s face it, as every wrapper it has its limits. What I’d like to see is for Swift style null safety to be added to Java. I’ve tried a variety of languages, but Swift’s vision for null safe types feels much more thought out than, for example, Kotlin’s. Casually, it also falls perfectly in line with Java’s current approach.

Let’s look at some examples:

Before Java 8:

public Integer parseInt(String in){
    try {
        return Integer.parseInt(in);
    }catch (NumberFormatException e){
       return null;
    }
}

I know it’s not the best example, but it serves my purpose quite nicely. I’ve seen this type of code so many times and in my opinion throwing an exception is always to be preferred to returning null values.

Java 8:

public Optional<Integer> parseInt(String in){
    try {
        return Optional.of(Integer.parseInt(in));
    }catch (NumberFormatException e){
        return Optional.empty();
    }
}

A much better approach, but it’s quite verbose.

Swift:

func parseInt(input: String) -> Int? {
    return Int(input)
}

Kotlin:

fun parseInt(input: String): Int? {
     return input.toIntOrNull()
}

Swift and Kotlin look pretty similar, but let’s try to print the result for Java 8+, Swift and Kotlin:

Optional[123] // Java 8+ with input 123

Optional(123) // Swift with input 123

123 // Kotlin with input 123

As you can see for Swift and Java the nullable value is actually wrapped in an optional, simply in Swift, it’s hidden better while for Kotlin it doesn’t hold the same meaning. What I’d like to see is Swift’s style null safety, not Kotlin’s!

A more in-depth comparison can be found here:
Swift Optional and Kotlin Nullable— A comparison

I have many more features to suggest, but this post already feels long enough. I have voluntarily excluded features that are already scheduled for java such as template generics, green threads, value types and much more!


Originally posted on r/java on December 28, 2020.



Previous Post
Flutter Web Engine: Make the html page adapt to the canvas' size instead of hiding it
Next Post
WhatsappWeb4j - A standalone WhatsappWeb library