Skip to content

Instantly share code, notes, and snippets.

@mvitz
Last active October 27, 2021 06:32
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 mvitz/3de064fb3e1c91c4f0bb6163d057b4f7 to your computer and use it in GitHub Desktop.
Save mvitz/3de064fb3e1c91c4f0bb6163d057b4f7 to your computer and use it in GitHub Desktop.
Examples for JDK/Java features introduced between JDK 11 and 17
package com.innoq.java17;
import java.util.ArrayList;
import java.util.List;
public class JugAugsburg {
// Switch Expressions
static void switchExpressions() {
int i = 3;
String result = switch (i) {
case 1 -> "One";
case 2 -> "Two";
default -> "Other";
};
System.out.println(result);
}
// Helpful NPE
static class Sum {
Integer a, b, c;
public Sum(Integer a, Integer b, Integer c) {
this.a = a;
this.b = b;
this.c = c;
}
public int sum() {
return a + b + c;
}
}
static void helpfullNullPointerException() {
try {
System.out.println(new Sum(1, null, 3).sum());
} catch (NullPointerException e) {
e.printStackTrace(System.out);
}
}
// Text Blocks
static void textBlocks() {
var name = "Michael";
var text = """
First Line \
Second Line \s
Third Line
{
"someProperty": 1,
"foo": true,
}
${name}
%s
""";
System.out.println(String.format(text, name));
System.out.println(text.formatted(name));
}
// Records
record Stats(int min, int max, List<Integer> foo){
Stats {
if (max < min) {
throw new IllegalStateException("Min must be lower than max");
}
}
public int min() {
return min + 3;
}
public Stats min(int i) {
return new Stats(i, this.max, this.foo);
}
public static Stats of(int min, int max) {
return new Stats(min, max, List.of());
}
public int bar() {
return min + max;
}
};
static void records() {
final Stats stats = new Stats(1, 10, new ArrayList<>());
System.out.println("Min: " +stats.min());
stats.foo.add(3);
System.out.println(stats);
System.out.println(stats.equals(new Stats(2, 10, List.of())));
System.out.println(stats.equals(new Stats(1, 10, List.of())));
}
// Pattern matching (instanceof)
static void instanceOfPatternMatching() {
Object o = "Michael";
if (o instanceof String s) {
// var s = (String) o;
var result = s.trim();
}
Object o2 = "Michael";
if (!(o instanceof String s)) {
throw new IllegalStateException("Not a string");
}
s.trim();
}
// Pattern matching in switch
static void switchPatternMatching() {
Object o = null;//"Michael";
var result = switch (o) {
case String s && s.length() < 8 -> "Is a string shorter than 8" + s.isEmpty();
case String s -> "Is a string " + s.length();
case Integer i -> "Is an integer";
case null -> "Its null";
default -> "Something other";
};
System.out.println(result);
}
// sealed classes
sealed interface MyInterface permits Michael, Jug {}
static non-sealed class Michael implements MyInterface {}
static final class Jug implements MyInterface {}
static void switchPatternMatchingWithSealedClasses() {
MyInterface m = new Michael();
var result = switch (m) {
case Michael n -> "Michael";
case Jug j -> "JUG";
case null -> "null";
// no default needed because switch is exhaustive
};
System.out.println(result);
}
public static void main(String[] args) {
// JUG Augsburg
switchExpressions();
helpfullNullPointerException();
textBlocks();
records();
instanceOfPatternMatching();
switchPatternMatching();
switchPatternMatchingWithSealedClasses();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment