This document summarizes Joseph Kuo's presentation on new features in Java 18 and 19. It discusses survey results on the state of the Java ecosystem from TIOBE Index, GitHub Octoverse, and Stack Overflow. It then covers new language features including simple web server, UTF-8 default encoding, code snippets in JavaDoc, pattern matching for switch/instanceof, record patterns, vector API, virtual threads, and preview features.
13. Most Demanded PL in 2022
- Published by DevJobsScanner on June 29,
2022
- From Oct-2021 to Jun-2022
- They have analyzed more than 7M
developer jobs, and only picked the job
offers that explicitly required a
programming language. Job offers with 4+
language or stack requirements were
discarded.
https://www.devjobsscanner.com/blog/top-8-most-demanded-languages-in-2022/
18. 2022 State of Java Ecosystem
- New Relic first published in March 2020
- 2022 state was published in April 2022
- Based on data gathered from millions of
applications providing performance data
Source: https://newrelic.com/resources/report/2022-state-of-java-ecosystem
25. Simple Web Server
- JEP 408 (JDK 18)
- A command-line tool to start a minimal web
server that serves static files only
- Useful for prototyping and testing so no
CGI / servlet-like functionality is available
- Provide a default implementation via the
command line together with a small API for
programmatic creation and customization
27. Features & Limitation
- Only idempotent HEAD & GET
- Others receive a 501 Not implemented or a
405 Not Allowed response
- All files (except for links & hidden files) are
listed if no index file in the directory
- Only support HTTP 1.1, no HTTPS support
31. UTF-8 by Default
- JEP 400 (JDK 18)
- In JDK 17 and earlier, the default charset is
determined when the Java runtime starts
- More predictable and portable when their
code relies on the default charset
- Clarify where the standard Java API uses the
default charset
- Standardize on UTF-8 throughout the
standard Java APIs, except for console I/O
32. UTF-8 by Default
A Japanese text file encoded in UTF-8 on
macOS:
java.io.FileReader("hello.txt")
- macOS -> “こんにちは”
- Windows (en-US) -> “ã?“ã‚“ã?«ã?¡ã? ”
- Windows (ja-JP) -> “縺ォ縺。縺ッ”
33. Default Charset APIs
- InputStreamReader, OutputStreamWriter,
FileReader, FileWriter, & PrintStream:
constructors to create readers, writers, &
print streams to encode or decode
- Formatter & Scanner: constructors whose
results use the default charset.
- URLEncoder & URLDecoder: deprecated
methods that use the default charset
34. UTF-8 by Default
- Charset.defaultCharset() is "UTF-8"
unless configured
- file.encoding=COMPAT: default charset will
be like JDK 17 and earlier (OS, locale)
- Recommand to use
java –Dfile.encoding=UTF-8
for JDK 8 ~ 17
36. Code Snippets in JavaDoc
- JEP 413 (JDK 18)
- Introduce an @snippet tag for JavaDoc
- Facilitate the validation of source code
fragments by providing API access
- Enable modern styling & automatic linkage
of names to declarations
- Enable better IDE support for creating and
editing snippets
37. Current Approach
* <pre>{@code
* int sum = widgets.stream()
* .filter(w -> w.getColor() == RED)
* .mapToInt(w -> w.getWeight())
* .sum();
* }</pre>
->
38. Shortcomings
- No way to reliably detect and validate code
fragments so errors can occur
- No syntax highlighting
- Cannot contain '*/', HTML markup, and link
tags to link other APIs
44. Pattern Matching for switch
- JEP 420 (JDK 18), JEP 427 (JDK 19)
- Expand the expressiveness and applicability
of switch by allowing patterns in case labels
- Allow null case of switch when desired
- Increase the safety of switch statements by
requiring to cover all possible input values
45. Pattern Matching for instanceof
if (o instanceof String s) {
System.out.println("Str: " + s);
} else if (o instanceof Integer i && i < 100) {
System.out.println(”Small Int: " + i);
} else if (o instanceof Integer i) {
System.out.println("Int: " + i);
} else if (o instanceof Long l) {
System.out.println("Long: " + l);
} else {
System.out.println("Obj: " + o);
}
46. Pattern Matching for switch
switch (o) {
case null -> System.out.println(”NULL");
case String s -> System.out.println("Str: " + s);
case Integer i when i < 100
-> System.out.println(”Small Int: " + i);
case Integer i -> System.out.println("Int: " + i);
case Long l -> System.out.println("Long: " + l);
default -> System.out.println("Obj: " + o);
}
47. Selector Expression Types
record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
switch (o) {
case null, String s -> System.out.println("Str: " + s);
case Color c -> System.out.println(”Color: " + c);
case Point p -> System.out.println(”Point: " + p);
case int[] ia -> System.out.println(”Array: " + ia);
default -> System.out.println("Obj: " + o);
}
49. Record Patterns
- JEP 405 (JDK 19)
- Extend pattern matching to express more
sophisticated, composable data queries
- Do not change the syntax or semantics of
type patterns
50. Record Patterns
record Point(int x, int y) {}
if (o instanceof Point p) {
int x = p.x(); int y = p.y();
System.out.println(x + y);
}
->
if (o instanceof Point(int x, int y)) {
System.out.println(x + y);
}
51. Nested Patterns
record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) {}
record Rectangle(ColoredPoint uLeft, ColoredPoint dRight) {}
if (o instanceof Rectangle(ColoredPoint(Point p, Color c),
ColoredPoint dRight)) {
System.out.println(c); // print upperLeft color
}
53. Vector API
- JEP 417 (JDK 18), JEP 426 (JDK 19)
- Express vector computations
- Clear and concise API running with reliable
runtime compilation and performance to
optimal vector instructions
54. Vector API
VectorSpecies<Float> S = FloatVector.SPECIES_PREFERRED;
void vectorComputation(float[] a, float[] b, float[] c) {
int upperBound = S.loopBound(a.length);
for (var i = 0; i < upperBound; i += S.length()) {
var m = S.indexInRange(i, a.length);
var va = FloatVector.fromArray(S, a, i);
var vb = FloatVector.fromArray(S, b, i);
var vc = va.mul(va).add(vb.mul(vb).neg();
vc.intoArray(c, i);
} // -> c[i] = (a[i]*a[i] + b[i]*b[i]) * -1.0f
}
56. Virtual Threads
- JEP 425 (JDK 19)
- Lightweight threads to dramatically reduce
the effort of writing, maintaining, and
observing high-throughput concurrent
applications
- Enable server applications written in the
simple thread-per-request style to scale w/
near-optimal hardware utilization
- Easy to use, debug and profile with existing
API and JDK tools
57. Thread-per-request Style
- Server application handles a request by
dedicating a thread for its entire duration
- Little's Law: For a given duration (latency),
number of requests that a server handles at
the same time (concurrency) must grow in
proportion to the rate of arrival (throughput)
- But thread number is limited because JDK
uses OS threads which are costly
58. Improve Scalability w/ Async
- Async style uses a separate set of I/O
methods not to wait for I/O operations
- Developers must break down logic into
small stages like lambda expressions, and
compose them into a sequential pipeline
- Each stage of a request may execute on a
different thread, and stack traces provide
no usable context
59. Preserving thread-per-request
- A Java thread was like an OS thread
- 為了使應用擴展,同時與平台保持協作,因此
有效率地改善實作,通過將大量虛擬執行緒映
射到作業系統執行緒,達成執行緒數量增長
- 一個虛擬執行緒會被臨時對映到載體執行緒池
中。一旦虛擬執行緒遇到阻塞操作,該虛擬執
行緒就會從載體執行緒中移除,而載體執行緒
可以執行另一個虛擬執行緒(新的或之前被阻
塞的)
65. Others
- JEP 416: Reimplement Core Reflection w/
Method Handles
- JEP 418: Internet-Address Resolution SPI
- JEP 421: Deprecate Finalization for Removal
(protected void finalize())
- JEP 422: Linux/RISC-V Port
- JEP 419, 424: Foreign Function & Memory
(FFM) API