I have to take a look at Java 18…

Yes Scala projects keep getting pulled back into the mire that is Java.

Does Java 18 have the features that made scala stand out over a decade ago?

Lets find out.

I have listed the features that jump out as either Scala like features, or wierd. I did Java for 20 years, before moving to mainly Scala for the last 5 or 6.

Java now has

Optional, Record, Pattern matching, sealed classes, multiline string Text blocks, var to infer type on local variables, copyOf helpers to create immutable collections. Also it had JShell for a REPL.

and has

java.util.Objects.requireNonNull(foo);

and lets not forget that scala 3 has enums…like Java (well kind of).

Java syntax for Scala devs

Switch

This is an expression, returning a value.

var d = switch (day) {         // var with inferred type since JDK 10
    case MONDAY, TUESDAY -> 1; // No colon, but the ->, and it returns a value
    default -> 2;
  }

var and for Loops

var l = List.of(1,2,3,8);
for (var i : l) {
  // stuff
}

Functions, higher functions and all that

SAM - Single Abstract Method, interfaces like Runnable, Callable which since Java 8 were the Lambda expression implementation.

Some examples below:

public class Foo {
  public static void main(String[] args) {
    try {
      new Thread( ()-> doRun() ).start(); // See the lambda!
    } catch (Exception ex) {
      System.out.println("Bad");
    }
  }
  static void doRun() {
    // Look at string.formatted....
    System.out.println("My thread is %s".formatted(Thread.currentThread.getName()));
  }
}
var fn = foo("a", true); // var infers Function<Integer, String>
System.out.print(fn.apply(1)); // call function which closed over value "a" above

// Function returning a function
static Function<Integer, String> foo(String s, boolean b) {
  return switch(s) {
    case "a" && b -> t -> "%s.%s".formatted(s,t);  // Notice the guard on the case.
    default       -> t -> s.toUpperCase();
  }
}
BiFunction<Integerr, Integer, Integer> = (a,b) -> a + b;

Scala types are everywhere, what about Java?

abstract class Foo<E extends SomeInterface> {

}

Array initial Value

double [][] a = { {1.0,1.1,1.2}, {2.0,2.1,2.2} }

JDK 21 is next long term support release, Sept 2023

JDK 19, Sept 2022

Loads of preview stuff, including

  • third preview of pattern matching for switch statements, when clause guard.

JDK 18, 2022

  • preview of switch statement pattern matching again
  • a web server for dev work
  • reflection rework
  • finalize deprecation
  • @snippet javadoc for code snippets

JDK 17, Sept 2021, Long term support

  • preview of switch statement pattern matching
  • deserialisation filters
  • Applets deprecated
  • foreign function and memory, rather than JNI
  • sealed classes and interfaces

JDk 16, March 2021

  • pattern matching on instanceof
  • preview of sealed classes
  • Alpine linux support
  • Record classes
  • jpackage native packager

JDK 15, Sept 2020

  • .isEmpty added
  • multi line string literals added. Text Blocks!
  • nashhorn javascript engine removed.

JDK 14, March 2020

  • preview of records

JDK 13,12, 11, 10, 9

  • Motif look and feel gone. Wow.
  • removal of web start
  • removal of server jre
  • JavaFx spit out
  • java mission control not in jdk
  • ZGC introduced (low latency parallel GC)
  • var used for local params with type interence.
  • remove Java EE and Corba modules from JDK.
  • Optional.orElseThrow
  • helpers to create immutable collections. copyOf
  • JVM modified to recognise when in Docker (JDK 10)
  • JShell is a REPL