Skip to content

Commit caea3c9

Browse files
committed
JDK 21: added API changes
1 parent dcf8eb0 commit caea3c9

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,6 @@ jshell --enable-preview
200200
* [Rewrite - Migrate to Java 17](https://docs.openrewrite.org/running-recipes/popular-recipe-guides/migrate-to-java-17)
201201
* Presentations:
202202
* [The Amazing Features of Modern Java - Venkat Subramaniam](https://youtu.be/nlZe-y2XvQY)
203+
* Java Projects:
204+
* [Project Leyden - Capturing Lightning in a Bottle](https://www.youtube.com/watch?v=lnth19Kf-x0&ab_channel=Java)
203205

java-21/README.md

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
2222

2323
## Features
2424

25-
* Virtual threads
25+
* **Virtual Threads**
2626
* promotion to standard
2727
* changed to make virtual threads always support thread-local
2828
* in preview releases was possible to create a virtual thread without thread-local support
2929
* flag `jdk.traceVirtualThreadLocals` to show the strack trace when a virtual threads sets a value in thread-local variable
30-
* Scoped values
30+
* **Scoped Values**
3131
* promoted from incubated to preview feature
3232
* moved from pacote `jdk.incubator.concurrent` to `java.util.concurrent`
33-
* Structured concurrency
33+
* **Structured Concurrency**
3434
* promoted from incubated to preview feature
3535
* moved from pacote `jdk.incubator.concurrent` to `java.util.concurrent`
3636
* changes from JDK 19 and 20:
3737
* changed method `StructuredTaskScope::fork` to return a `Subtask` instanceof of a `Future`
3838
* the sealed interface `Subtask` extends `Supplier<T>`
3939
* the method `Subtask::get` behaves exaclty like `Future::resultNow`
40-
* calling the method `Subtask::get` never blocks, it throws an `IllegalStateException` whe calling before `join` or when the subtask has not completed successfully
41-
* Record patterns
40+
* calling the method `Subtask::get` never blocks, it throws an `IllegalStateException` when calling before `join` or when the subtask has not completed successfully
41+
* **Record Patterns**
4242
* promotion to standard
4343
* the main change is remove the support for record pattern in header of an enhanced for loop
44-
* Pattern matching for `switch`
44+
* **Pattern Matching for `switch`**
4545
* promotion to standard
4646
* main changes from last JEPs:
4747
* removed parenthesized patterns (`case int i when (i > 0)`)
4848
* allow qualified enum constants as case constants (`case MyEnum.ITEM`)
4949
* exhaustiveness and compatibility:
50-
* compiler will only require exhaustiveness if the switch uses any pattern, null label or if selector expression isn't from a legacy type (char, byte, short, int, Character, Byte, Short, Integer, String or a enum)
50+
* compiler will only require exhaustiveness if the `switch` uses any pattern, `null` label or if selector expression isn't from a legacy type (`char`, `byte`, `short`, `int`, `Character`, `Byte`, `Short`, `Integer`, `String` or a enum)
5151
* improved switch to support pattern matching for types (like `instanceof`)
5252
* support for `null` case
5353
* support for guards where we can use a boolean expression (`case String s when s.length > 10`)
@@ -56,7 +56,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
5656
* fall through:
5757
* `switch` with type pattern doesn't support falling through
5858
* if using case label with `:`, we must end the block with `break` or `yield` (`case String s: ...; break;`, `case String s: ...; yield s.length();`)
59-
* String templates:
59+
* **String Templates**
6060
* improve the string with embedded expressions and template processors
6161
* goals:
6262
* simply the creation and expression of string with computed computed
@@ -153,8 +153,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
153153
StringTemplate st = StringTemplate.RAW."Test: \{null}";
154154
var message = STR_NULL_SANITIZER.process(st);
155155
```
156-
157-
* Sequenced Collections
156+
* **Sequenced Collections**
158157
* new interfaces to define a common way to iterate throught sequenced collections (list, sets and maps)
159158
* [collections type hierarchy with new interfaces](https://cr.openjdk.org/~smarks/collections/SequencedCollectionDiagram20220216.png)
160159
* sequenced collection:
@@ -190,7 +189,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
190189
* `unmodifiableSequencedCollection`
191190
* `unmodifiableSequencedSet`
192191
* `unmodifiableSequencedMap`
193-
* Unnamed classes and instance main methods
192+
* **Unnamed Classes and Instance Main Methods**
194193
* a.k.a. relaxed launch protocol
195194
* facilitate the writing of first programm for students without needing to know another features designed for large programs
196195
* unnamed class:
@@ -235,7 +234,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
235234
* `static void main()`
236235
* `void main(String[])`
237236
* `void main()`
238-
* Unnamed patterns and variables
237+
* **Unnamed Patterns and Variables**
239238
* goals:
240239
* improve the readability of record patterns by ediling unnecessary nested patterns
241240
* improve the maintanability of code by identifying variables that must be declared but will not be used
@@ -246,7 +245,7 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
246245
* it never shadows another variable, can be used many times
247246
* it can be used when:
248247
* declaring a local variable (`int _ = q.remove()`)
249-
* resource specification of a try-with-resources (`try (_ = ScopedContxt.acquire())`)
248+
* resource specification of a try-with-resources (`try (var _ = ScopedContxt.acquire())`)
250249
* exception parameter in a catch clause (`catch (NumberFormatException _)`)
251250
* header of an enhanced for loop (`for (int i = 0; _ = sideEffect(); i++)`)
252251
* header of an enhanced for loop (`for (Order _ : orders)`)
@@ -261,19 +260,60 @@ To run each example use: `java --enable-preview --source 21 <FileName.java>`
261260
* we can use it in any type pattern
262261
* `p instanceof Point _`
263262
* `case Point _`
264-
* APIs:
265-
* improve `Thread.sleep(millis, nanos)` to actually perform sleep with sub-millisecond time
266-
* [`java.net.http.Http Client` is now `AutoCloseable`](https://jdk.java.net/21/release-notes#JDK-8267140) and new methods were added to close/shutdown the connection pool.
267263

264+
### API
265+
266+
* changes in `Thread` class:
267+
* improve `Thread::sleep(millis, nanos)` to actually perform sleep with sub-millisecond time
268+
* added method `Thread::sleep(Durantion)`, `Thread::join(Durantion)` and `Thread::isVirtual`
269+
* added methos in `Future` that doesn't declare any checked exception (but will throw a `IllegalStateException` if is not completed):
270+
* `T resultNow()`
271+
* `Throwable exceptionNow()`
272+
* `State state()`: returns the current state of the task (values: `RUNNING`, `SUCCESS`, `FAILED`, `CANCELLED`)
273+
* added method `String::indexOf(String substr, int startIndex, int endIndex)` to find substring withing a index range
274+
* added method `StringBuilder::repeat(String str, int numberOfTimes)` to repeat a string # times
275+
* new methods to create collection with initial capacity:
276+
* `HashMap::newHashMap(int)`
277+
* `HashSet::newHashSet(int)`
278+
* `LinkedHashMap::newLinkedHashMap(int)`
279+
* `LinkedHashSet::newLinkedHashSet(int)`
280+
* added method `InputStream::transferTo(OutputStream)` to read the stream and send to the received output stream
281+
* [`java.net.http.Http Client` is now `AutoCloseable`](https://jdk.java.net/21/release-notes#JDK-8267140) and new methods were added to close/shutdown the connection pool.
282+
* classes `ExecutorService` and `ForkJoinPool` changed to implement `AutoCloseable`
283+
* added methods to `Math` class:
284+
* `int ceilDiv(int, int)`
285+
* `long ceilDiv(long, int)`
286+
* `long ceilDiv(long, long)`
287+
* `int ceilDivExact(int, int)`
288+
* `long ceilDivExact(long, long)`
289+
* `int ceilMod(int, int)`
290+
* `long ceilMod(long, int)`
291+
* `long ceilMod(long, long)`
292+
* division that throws `ArithmeticException` in case of overflow:
293+
* `int divideExact(int, int)`
294+
* `long divideExact(long, long)`
295+
* `int floorDivExact(int, int)`
296+
* `long floorDivExact(long, long)`
297+
* methods to clamp a value in a range for each type `int`, `long`, `float` and `double`:
298+
* `clamp(value, min, max)`
299+
* added method `BigInteger::parallelMultiply(BigInteger)`
300+
301+
Deprecations:
302+
303+
* `Object::finalize`
304+
* constructors in `Integer` class: `Integer(int)` and `Integer(String)`
305+
* should use `Integer::valueOf(int)` and `Integer::valueOf(String)`
268306

269307
## Links
270308

271-
* [JDK 21 Jeps](https://openjdk.org/projects/jdk/21/)
309+
* [JDK 21 JEPs](https://openjdk.org/projects/jdk/21/)
272310
* [JDK 21 Early Access Docs](https://download.java.net/java/early_access/jdk21/docs/api/)
273311
* [JEP 444: Virtual Threads Arrive in JDK 21, Ushering a New Era of Concurrency](https://www.infoq.com/news/2023/04/virtual-threads-arrives-jdk21/)
274312
* [Upgrading from Java 17 to 21](https://www.youtube.com/watch?v=5jIkRqBuSBs)
275313
* Presentations:
276314
* [Java Virtual Threads - Oracle DevLive Level Up](https://www.youtube.com/watch?v=MOgynY7VIJI)
277315
* [The Challenges of Indroducing Virtual Threads to the Java Platform](https://www.youtube.com/watch?v=WsCJYQDPrrE)
278-
* [Java 21 new feature: Virtual Threads](https://www.youtube.com/watch?v=5E0LU85EnTI)
316+
* [Java 21 New Feature: Virtual Threads](https://www.youtube.com/watch?v=5E0LU85EnTI)
317+
* [Java 21 API New Features](https://www.youtube.com/watch?v=4mPd2eL0wYQ)
318+
* [Java 21 Brings Full Pattern Matching](https://www.youtube.com/watch?v=QrwFrm1R8OY)
279319

0 commit comments

Comments
 (0)