Skip to content

Instantly share code, notes, and snippets.

@rtomyj
Last active March 21, 2024 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtomyj/5b265d91b5dbcf9b5e73c1f50a2c152a to your computer and use it in GitHub Desktop.
Save rtomyj/5b265d91b5dbcf9b5e73c1f50a2c152a to your computer and use it in GitHub Desktop.

Java seems to be prefering immutability going forward

Reaons - Immutablility is thread safe, more performant and more reliable (as in there should be less unexpected behavior since there will be no side effects when calling methods)

What is immutability? It just means it the value cannot change. Immutable collections cannot have their items modified.

A lot of Collection convenience methods will return an immutable collection

list.of();
Collections.singletonList();

To create a mutable collection from an immuatble one (in case you really, really needed one), use the collections constructor

new ArrayList<>(List.of("", "e3"));

Immutable lists should stick to using streams as other APIs (like the Collections package) might try to modify the collection

Available in JDK 14

Features / Limitations

  • Records do not have default no args constructor
  • Instances of Records are immutable - you will have to create a new instance if you are modifying a field
  • Record classes cannot be extended - you must extend via composition
  • By default, Java will create the following methods (lessons need for Lombok)
    • equals()
    • hashCode()
    • toString()
    • getters for fields (though they are more field accessors than getters)
    • does not create setters as Records are immutable

Similar to Kotlins data classes

public record Album(String name, Genre genre, Artist artist) {}

Can leverage Lombok for Builder pattern on Records

Available in JDK 17

Purpose

Sealed classes/interfaces specifically define which other classes can be its children

// only Nissan, Ford, Honda sub classes can extend Car
public sealed abstract class Car permits Nissan, Ford, Honda {
    public abstract String make();
}

// only Red, Yellow, Blue sub classes can extend Color
public sealed interface Color permits Red, Yellow, Blue {
    String getHex();
}

Children of sealed classes should be marked final, sealed or non-sealed

  • Marking a class that extends a sealed class as sealed, applies the same effect on it: Only classes specified after the permits clause are allowed to extend it.
  • non-sealed just "breaks the seal", so the effect doesn't have to be carried on down the hierarchy. The extending class is open (again) for being extended by unknown subclasses itself.
  • final is effectively the same as sealed without any class specified after the permits clause. Notice that specifying nothing after permits is not possible, so sealed cannot replace final.
public non-sealed class Nissan extends Car {}


public final class Ford extends Car {}

Available in JDK 21 (in preview)

Purpose

Allows usage of in scope variables in a string

final String name = "Javi";
final String greeting = STR."Hello \{name}"

Works with text-blocks

Available in JDK 14

Using tripple quotes, we can define a string with line breaks and other special characters without escaping them

"""
{
    "AD": {
        "currency": {
            "primary": "EUR"
        },
        "iso": {
            "code2": "AD",
            "code3": "AND",
            "num": "020"
        },
        "languages": [
            "ca"
        ],
        "name": "Andorra",
        "region": "Europe"
    }
}""";

Available in JDK 21

Project Loom

In memory (virtual) threads that are managed by the JVM as opposed to the old threads which were managed by the OS

Does not matter if virtual thread is blocked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment