Java 17 Features (briefly) Explored

Java Release Cycle

  • "Fast" release cycle is every 6 months. Every march and September.
  • "LTS" release cycle is every 2 years (beginning with Java 21

8 months before this is obsolete!

Major Language Features

  • Sealed Classes
  • switch can be used as statement or expression
  • Pattern Matching on switch expressions/statements
  • Records
  • Text Blocks
  •  Local Type Inference (ok this is Java 11, but I rarely see it used)

Local Type Inference

// local variable initialization 
Map<String, Object> attributes = 
	getAllAttributes(pubSubMessage.topic(), eventObj, pubSubMessage.id());
var attributes = 
	getAllAttributes(pubSubMessage.topic(), eventObj, pubSubMessage.id());

// for loops
public EntityWithEvents<I, ESE> add(Event... events) {
        for (Event event : events) {
        
        for (var event: events) {}
        
for (int i = 0; i < EVENTS_PER_ROW; i++) {

for (var i = 0; i < EVENTS_PER_ROW; i++) {

// try with resources
try (ServerSocket serverSocket = new ServerSocket(0)) 

try (var serverSocker = new ServerSocker(0))

Text Block

var multiLineString = """
		In Java, strings are objects too,
		They're created with the keyword "new"
		You can use them, concatenate,
		with the plus sign they'll inflate,
		to make a sentence, long and true.
        """;

Using Switch as an Expression

Day day = Day.WEDNESDAY;
    
    int numLetters = switch (day) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            System.out.println(6);
            yield 6;
        case TUESDAY:
            System.out.println(7);
            yield 7;
        case THURSDAY:
        case SATURDAY:
            System.out.println(8);
            yield 8;
        case WEDNESDAY:
            System.out.println(9);
            yield 9;
        default:
            throw new IllegalStateException("Invalid day: " + day);
    };
    System.out.println(numLetters);

Records

package com.wayfair.canon.loader.events;

import com.fasterxml.jackson.annotation.JsonProperty;

public record SalesforceAccountEvent(
    @JsonProperty("CreatedDate")
    String modifiedTime,
    @JsonProperty("CreatedById")
    String user,
    @JsonProperty("BaID__c")
     String baid,
    @JsonProperty("Name__c")
     String companyName,
    @JsonProperty("Status__c")
     String status,
    @JsonProperty("Industry__c")
     String industry,
    @JsonProperty("Domain__c")
     String registeredDomain,
    @JsonProperty("Region__c")
     String region,
    @JsonProperty("Company_state__c")
     String state,
    @JsonProperty("Phone__c")
     String phone
) implements SalesforceEvent { }

Sealed Classes

Java 17 Features (briefly) Explored

By Kyle Boon

Java 17 Features (briefly) Explored

  • 30