Java 17: Here’s a juicy update on everything that’s new

Sealed Classes — JDK 17 JEP 409

  • provides a more declarative way than access modifiers to restrict the use of superclass
  • support future enhancements in pattern matching to avoid exhaustive analysis of patterns
package com.domain.shapes;public abstract sealed class Shape permits Circle,Square, Rectangle {
}
public final class Circle extends Shape {
}
public non-sealed class Square extends Shape {
}
public sealed class Rectangle extends Shape permits TransparentRectange,OpaqueRectangle {
}
public final class OpaqueRectangle extends Rectangle {
}
public final class TransparentRectange extends Rectangle {
}

Text Blocks — JDK 17 JEP 378

String html = "<html>\n" +
"<body>\n"+
"<p>Test</p>\n"+
"</body>\n"+
"</html>\n";
String html = """
<html>
<body>
<p>Test</p>
</body>
</html>
""";

Pattern Matching for instanceof — JDK 16 JEP 394

if(obj instanceof String) {
String s = (String) obj;
}
if(obj instanceof String s) {
//use s
} else {
// s is out of scope here
}

Switch Expressions (JDK 14 JEP 361)

raditional case ... : labels //with fall through
New case ... -> labels //without fall through
int  numLetters;
switch(day)
{
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numLetters = 8;
case WEDNESDAY:
numLetters = 9;
break;
default:
throw new IllegalArgumentException(" Not a day: " + day);
}
return numLetters;
return switch(day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
}

Pattern Matching for switch Preview — JDK 17 JEP 406

  • allow patterns to appear in case labels
  • introduces case null statement
static String formatter(Object o){
String formatted = "unknown";
if(o instanceof Integer i){
formatted = String.format("int %d",i);
} else if ( o instanceof Long l){
formatted = String.format("long %d",l);
}
return formatted;
}
static String formatter(Object o){
return switch(o){
case null -> "null";
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
default -> o.toString();
};
}

Records — JDK 16 JEP 395

record range(int start, int end){}
public final class point{
final int x;
final int y;
}
public final class point{
final int x;
final int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
point point = (point) o;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "point{" +
"x=" + x +
", y=" + y +
'}';
}
public int x(){
return x;
}
public int y(){
return y;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ideas2IT

Ideas2IT

We’re a product engineering firm. Our work is cutting-edge, be it in AI-ML, Cloud, DevOps, or IIoT, for an enviable set of clients. Visit www.ideas2it.com.