Jewel Java

Java Software Engineer

Line of Business Public

Merlin Bögershausen / @mboegie

What is Amber again?

Amber

JDK Projects?

A Project is a collaborative effort to produce a specific artifact, which may be a body of code, or documentation, or some other material.

— OpenJDK Bylaws section 6

Project Amber?

The goal of Project Amber is to explore and incubate smaller, productivity-oriented Java language features

— Project Amber main page

JEP?

An enhancement is an effort to design and implement a nontrivial change to the JDK code base or to do some other kind of work whose goals, progress, and result are worth communicating broadly

— JEP 1

Ambers' Syntactic Sugar

Bilder PPT BigDateMMs

JEP 286

List<String> lineList = new ArrayList<String>();
// less duplication
var lines = new ArrayList<String>();
final var readLines = lines.size();

JEP 323

BiConsumer<Consumer<String>, String> sink
    = (x, y) -> x.accept(y);
// now with Annotations!
BiConsumer<Consumer<String>, String> safeSink
    = (@Nonnull var x, var y) -> x.accept(y);

JEP 378

Multi-line Text Blocks landed in Java 11

String helloWorldResponse = """
    <html>
        <body>
            <p>Hello, world</p>
        </body>
    </html>
    """;

JEP 361

Switch Expressions landed in Java 14

int j = switch (day) {// enum Days {MONDAY, …}
  case MONDAY, WEDNESDAY -> 0;
  case TUESDAY -> 1;
  default -> {
    int k = day.toString().length();
    yield k;
  }
};

JEP 361

no more NullPointerExceptions 🥳

int j = switch (day) {// enum Days {MONDAY, …}
  case null, MONDAY -> 0;
  case TUESDAY -> 1;
  default -> {
    int k = day.toString().length();
    yield k;
  }
};

JEP 361

exhaustive, default or compilation error 🥳

boolean goToWork = switch (day) {// enum Days {MONDAY, …}
  case null, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY -> true;
  case SATURDAY, SUNDAY -> false;
};

Type Enhancements

Bilder PPT Scribble

JEP 359

Records landed in Java 16

record Range(int low, int high) {
  Range {
    if (low > high)
      throw new IllegalArgumentException(/*...*/);
  }
  int lenght() { return high - low; }
}

JEP 359

normal final class ⇒ usual overloading

record Range(List<Integer> values) {
  Range(List<Integer> values) {
    this.values = List.copyOf(values);
  }
  Range(Integer i1, Integer i2) {
    this(List.of(i1,i2));
  }
}

JEP 409

Sealed Classes landed in Java 17

sealed interface Shape {
  sealed class Cornered implements Shape {}
  final class Weirdo implements Shape {}
}

JEP 409

different compile Unit ⇒ permits needed

sealed interface Cornered implements Shape
    permits Rectangle, Square {}

final class Rectangle implements Cornered {}
final class Square() implements Cornered {}

JEP 409

sealed have to be propagated

sealed interface Shape {
    non-sealed class Circular implements Shape {}
    sealed class Cornered implements Shape {}
    final class Rectangle implements Cornered {}
    record Square(int h) implements Cornered {}
}

Pattern Matching

Bilder PPT LottoCode

JEP 394

landed in Java 16

if (o instanceof Triangle t) {
    System.out.println("Triangle with area: "
        + t.calculateArea());
} else {
    System.out.println("Not a triangle);
}

JEP 394

Mixing of patterns and operators possible

if (o instanceof Triangle t) {
    System.out.println("Area: " + t.calculateArea());
} else if (!(o instanceof String str)) {
    System.out.println("Not Triangle or String ");
} else {
    System.out.println("String value is:" + str);
}

JEP 427

3rd Preview in Java 19

switch (s) {
  case Triangle t -> System.out.println("Triangle");
  case Square s -> System.out.println("Square");
  default -> System.out.println("Something");
}

JEP 427

Guarded Patterns with when

switch (s) {
  case Triangle t when t.calculateArea() > 100 ->
    System.out.println("Large");
  case Triangle t -> System.out.println("Small");
  default -> System.out.println("Non-triangle");
}

JEP 405

1st Preview in Java 19

record Point(int x, int y) {}

void printSum(Object o) {
  if (o instanceof Point(int x, var y)) {
    System.out.println(x+y);
  }
}

From the Dreams

Snooze

JEP 302

Lambda Leftovers no target, yet

BiFunction<Integer, String, String> f
    = (i, _) -> String.valueOf(i);

Concise Method Bodies

class User {
    private final String name;
    Integer nameLength() = name::length;
}

String Template

Jim Laskey and Brian Goetz Sep. 2012

int x = 10, y = 20;
TemplatedString s = "\{x} + \{y} = \{x + y}";
     // Concatenation: 10 + 20 = 30

In Action

Binary tree v2

Base

sealed interface Tree { Tree delete(int value); }
record SubTree(Tree left, int value, Tree right)
    implements Tree {}
record EmptyTree() implements Tree {}

Java OO

// SubTree#delete(int)
if (value == v) {
  if (left instanceof EmptyTree
        && right instanceof EmptyTree) return new EmptyTree();
  else if (left instanceof EmptyTree
        && right instanceof SubTree) return right;
  else if (left instanceof SubTree
        && right instanceof EmptyTree) return left;
  else if (left instanceof SubTree l
        && right instanceof SubTree)
    return l.insertInRightTree(right);
  else throw new IllegalStateException(
            "This should not be possible");
} else //...

Java FP

// TreeOperations#delete(int)
return switch (tree) {
  case EmptyTree e, null -> e;
  case SubTree(Tree l,Integer v,Tree r)
    when contains(l, value) -> delete(l, value);
  case SubTree(Tree l,Integer v,Tree r)
    when contains(r, value) -> delete(r, value);
  case SubTree(EmptyTree l,Integer v,EmptyTree r)
    when (v == value) -> new EmptyTree();
  case SubTree(SubTree l,Integer v,EmptyTree r)
    when (v == value) -> l;
  case SubTree(EmptyTree l,Integer v,SubTree r)
    when (v == value) -> r;
  case SubTree(SubTree l,Integer v,SubTree r)
    when (v == value) -> insertSubTree(r, l);
  case SubTree subTree -> subTree;
};

Contact

Me

@MBoegi // MBoegers

adesso

Infos // DevBlog

Jobs

qrcode adesso jobs

Image Credits