Os melhores novos recursos do Java 25



Os melhores novos recursos do Java 25

class Form {
    ultimate int space;
    public Form(int space) {
        if (space <= 0) throw new IllegalArgumentException("Space should be optimistic.");
        this.space = space;
    }
}

Agora diga que você quer ter um Rectangle aula. Em Java antes do JDK 25, você teria que extrair de alguma forma o cálculo para usá-lo no tremendous() chamada, geralmente usando um método estático:


// The previous method
class Rectangle extends Form {
    non-public static int checkAndCalcArea(int w, int h) {
        if (w <= 0 || h <= 0) {
            throw new IllegalArgumentException("Dimensions should be optimistic.");
        }
        return w * h;
    }

    public Rectangle(int width, int top) {
        tremendous(checkAndCalcArea(width, top)); // tremendous() needed to be first
        // ... constructor logic ...
    }
}

Este código é bastante desajeitado. Mas no Java 25 é mais fácil seguir sua intenção e executar o cálculo da área no Rectangle construtor:

class Rectangle extends Form {
    ultimate int width;
    ultimate int top;

    public Rectangle(int width, int top) {
        if (width <= 0 || top <= 0) {
            throw new IllegalArgumentException("Dimensions should be optimistic.");
        }
        int space = width * top;

        tremendous(space); // Earlier than 25, this was an error

        this.width = width;
        this.top = top;
    }
}

Importações de módulos no atacado

Outro recurso finalizado no JDK 25, JEP 511: Declarações de importação de módulospermite importar um módulo inteiro em vez de importar cada pacote um por um.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *