0% found this document useful (0 votes)
11 views

Pearsonjava 9 To 211720732430043

Uploaded by

snehapradhane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Pearsonjava 9 To 211720732430043

Uploaded by

snehapradhane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 99

Java 9 to 21: How to code like a pro

Henri Tremblay
Managing Director, Head of TS Imagine Canada
TS Imagine
@henri_tremblay

© Henri Tremblay 2015 ©2024 Henri Tremblay


Henri Tremblay

Alumni
• More or less made possible class mocking and proxying
• Coined the term “partial mocking”

2
o l d s h i nny
G o o d New &

8 21

3
Source code

https://github.com/henri-tremblay/java91011beyond

4
POLL

Which version of Java are you currently using?


Java < 8
Java 8
Java 9-10
Java 11
Java 12-16
Java 17
Java 18-20
Java 21
Java > 21
5
Breaks

6
Questions

?
7
Java Delivery Process

8
Delivery process

Java 8 Java 9 … Java 11 … Java 17 Java 18 … Java 20 Java 21

3 years 3 years 6 months 6 months 6 months

LTS every 3 years LTS every 2 years

Support up to 2030
Support up to 2026

LTS Normal Support up to 2029


9
POLL
Which build of Java are you currently using?
Oracle JDK
Oracle OpenJDK
Eclipse Adoptium Temurin (formerly AdoptOpenJDK)
Amazon Corretto
IBM J9
Azul Zing
Azul Zulu
Bellsoft Liberica JDK
Alibaba Dragonwell
RedHat
Microsoft
SAP
Something else
I don’t know
10
All these JVMs

Oracle works on the latest version


It will publish 2 patches until the next version is out
E.g.: 20.0.0 à 20.0.1 à 20.0.2 à 21.0.0
When a new version is out, the previous is now unsupported
Other vendors will backport fixes to the LTS versions and provide
new builds
Of course, whoever want to sell support for whatever version can

For more details see


https://medium.com/@javachampions/java-is-still-free-3-0-0-
ocrt-2021-bca75c88d23b

11
JXX
JCP: Java Community Process
Responsible of Java governance
JLS: Java Language Specification
This is a specification for the Java Language
JSR: Java Specification Request
Big piece of Java change submitted to JCP
JEP: Java Enhancement Proposal
Little pieces of Java change
https://openjdk.java.net/projects/jdk/ii/
12
Projects

Valhalla: Value types


Loom: New type of threads called Fiber
Panama: Improved IO
Amber: Developer productivity
Leyden: Improve startup time

https://openjdk.java.net/projects/
13
JEPs of projects

Amber:
JEP 361: Switch Expressions
JEP 389: Text Blocks
JEP 394: Pattern Matching for instanceof
JEP 395: Record
JEP 405: Record Patterns & Array Patterns (Preview)
JEP 406: Pattern matching for switch (Preview)
JEP 409: Sealed Classes

Valhalla:
JEP 374: Disable and Deprecate Biased Locking
JEP 390: Warnings for Value-Based Classes
JEP 401: Primitive Objects (Preview)
JEP 402: Unify the Basic Primitives with Objects (Preview)

Loom:
JEP 353: Reimplement the Legacy Socket API
JEP 373: Reimplement the Legacy DatagramSocket API

14
Trends

15
Trend #1

@Deprecated
is back
Stronger than ever
16
/**
* Counts the number of stack frames in this thread. The thread must
* be suspended.
*
* @return the number of stack frames in this thread.
* @throws IllegalThreadStateException if this thread is not
* suspended.
* @deprecated The definition of this call depends on {@link #suspend},
* which is deprecated. Further, the results of this call
* were never well-defined.
* This method is subject to removal in a future version of Java SE.
* @see StackWalker
*/
@Deprecated(since="1.2", forRemoval=true)
public native int countStackFrames();

17
Classes and methods removed

Java 8 Java 9 Java 10 Java 11

Unsafe.defineClass() Unsafe.defineClass()
removed
MethodHandles.defineClass()

18
Entire module removed

What any sane person will want his/her framework to support

Java 8 Java 9 Java 10 Java 11

import java.xml.bind --module-path containing


jaxb-api-2.4.0-xxx.jar
--add-modules javax.xml.bind

19
Trend #2

Encapsulation
The party is over

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to


make field private final byte[] java.lang.String.value accessible: module java.base
does not "opens java.lang" to unnamed module @2ff4acd0

20
Trend #3
Garbage Collector
Java < 8 Java 11 Java 21
• Serial • Epsilon • Generational Z
• Parallel • Z (Experimental)
• ParallelOld
• CMS Java 12
• iCMS • Shenandoah (Experimental)

Java 8 Java 14
• G1 • Remove CMS

Java 9 Java 15
• Removed iCMS • Shenandoah
• G1 (Default GC) • Z

21
Trend #4

Developer efficiency

Inference Pattern matching


// 71 characters
Map<String, String> map = new Map<String, String>();
map.put(“a”, “b”);
// 27 characters
var map = Map.of(“a”, ”b”);

22
Trend #5

Performance

23
Legend

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance
24
Java 9
(September 2017)

25
Modules
102: Process API Updates 238: Multi-Release JAR Files 273: DRBG-Based SecureRandom Implementations
110: HTTP 2 Client 240: Remove the JVM TI hprof Agent 274: Enhanced Method Handles
143: Improve Contended Locking 241: Remove the jhat Tool 275: Modular Java Application Packaging
158: Unified JVM Logging 243: Java-Level JVM Compiler Interface 276: Dynamic Linking of Language-Defined Object Models
165: Compiler Control 244: TLS Application-Layer Protocol Negotiation Extension 277: Enhanced Deprecation
193: Variable Handles 245: Validate JVM Command-Line Flag Arguments 278: Additional Tests for Humongous Objects in G1
197: Segmented Code Cache 246: Leverage CPU Instructions for GHASH and RSA 279: Improve Test-Failure Troubleshooting
199: Smart Java Compilation, Phase Two 247: Compile for Older Platform Versions 280: Indify String Concatenation
200: The Modular JDK 248: Make G1 the Default Garbage Collector 281: HotSpot C++ Unit-Test Framework
201: Modular Source Code 249: OCSP Stapling for TLS 282: jlink: The Java Linker
211: Elide Deprecation Warnings on Import Statements 250: Store Interned Strings in CDS Archives 283: Enable GTK 3 on Linux
212: Resolve Lint and Doclint Warnings 251: Multi-Resolution Images 284: New HotSpot Build System
213: Milling Project Coin 252: Use CLDR Locale Data by Default 285: Spin-Wait Hints
214: Remove GC Combinations Deprecated in JDK 8 253: Prepare JavaFX UI Controls & CSS APIs for 287: SHA-3 Hash Algorithms
215: Tiered Attribution for javac Modularization 288: Disable SHA-1 Certificates
216: Process Import Statements Correctly 254: Compact Strings 289: Deprecate the Applet API
217: Annotations Pipeline 2.0 255: Merge Selected Xerces 2.11.0 Updates into JAXP 290: Filter Incoming Serialization Data
219: Datagram Transport Layer Security (DTLS) 256: BeanInfo Annotations 291: Deprecate the Concurrent Mark Sweep (CMS) Garbage
220: Modular Run-Time Images 257: Update JavaFX/Media to Newer Version of GStreamer Collector
221: Simplified Doclet API 258: HarfBuzz Font-Layout Engine 292: Implement Selected ECMAScript 6 Features in Nashorn
222: jshell: The Java Shell (Read-Eval-Print Loop) 259: Stack-Walking API 294: Linux/s390x Port
223: New Version-String Scheme 260: Encapsulate Most Internal APIs 295: Ahead-of-Time Compilation
224: HTML5 Javadoc 261: Module System 297: Unified arm32/arm64 Port
225: Javadoc Search 262: TIFF Image I/O 298: Remove Demos and Samples
226: UTF-8 Property Files 263: HiDPI Graphics on Windows and Linux 299: Reorganize Documentation
227: Unicode 7.0 264: Platform Logging API and Service Deprecation
228: Add More Diagnostic Commands 265: Marlin Graphics Renderer
229: Create PKCS12 Keystores by Default 266: More Concurrency Updates Garbage Collector
231: Remove Launch-Time JRE Version Selection 267: Unicode 8.0
232: Improve Secure Application Performance 268: XML Catalogs Encapsulation
233: Generate Run-Time Compiler Tests Automatically 269: Convenience Factory Methods for Collections
235: Test Class-File Attributes Generated by javac 270: Reserved Stack Areas for Critical Sections
Developer efficiency
236: Parser API for Nashorn 271: Unified GC Logging
237: Linux/AArch64 Port 272: Platform-Specific Desktop Features
Performance

26
Modern mustache instantiation

Map<String, String> map = new HashMap<>() {{


put("key", "value");
}};

27
Immutable collections

Map<String, String> map =


Map.of("key", "value");

28
Slightly longer map instantiation

private Map<String, String> map = new HashMap<>();


{
map.put("key", "value");
}

29
Improved try-with-resource

ByteArrayOutputStream in = new ByteArrayOutputStream();

try(in) {

Instead of

try(ByteArrayOutputStream other = in) {

}
30
Interface private methods

public interface Printer {


private void print(String s) {
System.out.println(s);
}

default void printAll(String[] list) {


Arrays.stream(list).forEach(this::print);
}
}

31
Java SE Modules

Credits: Oracle
32
Modules

33
5 minutes break

34
Java 10
JSR 383 (March 2018)

35
Var!
JEP 286: Local-Variable Type Inference
JEP 296: Consolidate the JDK Forest into a Single Repository
JEP 304: Garbage-Collector Interface
JEP 307: Parallel Full GC for G1
JEP 310: Application Class-Data Sharing
JEP 312: Thread-Local Handshakes
JEP 313: Remove the Native-Header Generation Tool (javah)
JEP 314: Additional Unicode Language-Tag Extensions
JEP 316: Heap Allocation on Alternative Memory Devices
JEP 317: Experimental Java-Based JIT Compiler Deprecation
Garbage Collector
JEP 319: Root Certificates
Encapsulation
JEP 322: Time-Based Release Versioning Developer efficiency
Performance

36
Var inference 13

37
What is var?

Keywords
while, if, abstract, public, default,
class, enum (Java 5), _ (Java 9)
Restricted Keywords
open, module, to, with
Literals
true, false, null
Reserved identifier
var

38
Var inference 14

39
Java 11
JSR 384 (September 2018)

40
Cleanup!

JEP 181: Nest-Based Access Control


JEP 309: Dynamic Class-File Constants
JEP 315: Improve Aarch64 Intrinsics
JEP 318: Epsilon: A No-Op Garbage Collector
JEP 320: Remove the Java EE and CORBA Modules
JEP 321: HTTP Client (Standard)
JEP 323: Local-Variable Syntax for Lambda Parameters
JEP 324: Key Agreement with Curve25519 and Curve448
JEP 327: Unicode 10
JEP 328: Flight Recorder
JEP 329: ChaCha20 and Poly1305 Cryptographic Algorithms
JEP 330: Launch Single-File Source-Code Programs
JEP 331: Low-Overhead Heap Profiling
JEP 332: Transport Layer Security (TLS) 1.3 Deprecation
JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental) Garbage Collector
JEP 335: Deprecate the Nashorn JavaScript Engine
Encapsulation
JEP 336: Deprecate the Pack200 Tools and API
Developer efficiency
Performance

41
HTTP Client 24

42
Single file program Java11

43
Var in a lambda, Var inference bis 14

44
Java 12
JSR 386 (March 2019)

45
Switch expressions!

JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)


JEP 230: Microbenchmark Suite
JEP 325: Switch Expressions (Preview)
JEP 334: JVM Constants API
JEP 340: One AArch64 Port, Not Two
JEP 341: Default CDS Archives
JEP 344: Abortable Mixed Collections for G1
JEP 346: Promptly Return Unused Committed Memory from G1

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

46
Switch expressions
private boolean isWeekDay(DayOfWeek day) {
boolean weekDay;

switch(day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
weekDay = true;
break;
case SATURDAY:
case SUNDAY:
weekDay = false;
default:
throw new IllegalStateException("A new day was added in my week: " + day);
}
return weekDay;
}

47
Switch expressions
private boolean isWeekDay(DayOfWeek day) {
boolean weekDay;

switch(day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
weekDay = true;
break;
case SATURDAY:
case SUNDAY:
weekDay = false;
break;
default:
throw new IllegalStateException("A new day was added in my week: " + day);
}
return weekDay;
}
48
Switch expressions
private boolean isWeekDay(DayOfWeek day) {
boolean weekDay;

switch(day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> weekDay = true;
case SATURDAY, SUNDAY -> weekDay = false;
default -> throw new IllegalStateException("A new day was added in my week: " + day);
}

return weekDay;
}

49
Switch expressions
private boolean isWeekDay(DayOfWeek day) {
boolean weekDay = switch(day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> true;
case SATURDAY, SUNDAY -> false;
default -> throw new IllegalStateException("A new day was added in my week: " + day);
};

return weekDay;
}

50
Switch expressions
private boolean isWeekDay(DayOfWeek day) {
return switch(day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> true;
case SATURDAY, SUNDAY -> false;
default -> throw new IllegalStateException("A new day was added in my week: " + day);
};
}

51
Java 13
JSR 388 (September 2019)

52
Text Block!

JEP 350: Dynamic CDS Archives


JEP 351: ZGC: Uncommit Unused Memory
JEP 353: Reimplement the Legacy Socket API
JEP 354: Switch Expressions (Preview)
JEP 355: Text Blocks (Preview)

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

53
Switch expressions (still a preview)

private boolean isWeekDay(DayOfWeek day) {


return switch(day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> true;
case SATURDAY, SUNDAY -> false;
default -> {
if(onEarth) {
yield false;
}
yield true;
}
};
}

54
Switch expressions, two syntaxes

private boolean isWeekDay(DayOfWeek day) {


return switch(day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
yield true;
case SATURDAY:
case SUNDAY:
yield false;
};
}

55
Text Block (JEP 355)

String script = """


function hello() {
print("Hello, world");
}

hello();
""";

56
Text Block 16

57
5 minutes break

58
Java 14
JSR 389 (March 2020)

59
Big one

JEP 305: Pattern Matching for instanceof (Preview)


JEP 343: Packaging Tool (Incubator)
JEP 345: NUMA-Aware Memory Allocation for G1
JEP 349: JFR Event Streaming
JEP 352: Non-Volatile Mapped Byte Buffers
JEP 358: Helpful NullPointerExceptions
JEP 359: Records (Preview)
JEP 361: Switch Expressions (Standard)
JEP 362: Deprecate the Solaris and SPARC Ports
JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
JEP 364: ZGC on macOS
JEP 365: ZGC on Windows
JEP 366: Deprecate the ParallelScavenge + SerialOld GC Combination
JEP 367: Remove the Pack200 Tools and API Deprecation
JEP 368: Text Blocks (Second Preview) Garbage Collector
JEP 370: Foreign-Memory Access API (Incubator) Encapsulation
Developer efficiency
Performance

60
Helpful NPE 17

61
Pattern Matching for instanceof 18

62
Records 19

63
Packaging tools (package dir)

64
Java 15
JSR 390 (September 2020)

65
Previews

JEP 339: Edwards-Curve Digital Signature Algorithm (EdDSA)


JEP 360: Sealed Classes (Preview)
JEP 371: Hidden Classes
JEP 372: Remove the Nashorn JavaScript Engine
JEP 373: Reimplement the Legacy DatagramSocket API
JEP 374: Disable and Deprecate Biased Locking
JEP 375: Pattern Matching for instanceof (Second Preview)
JEP 377: ZGC: A Scalable Low-Latency Garbage Collector
JEP 378: Text Blocks
JEP 379: Shenandoah: A Low-Pause-Time Garbage Collector
JEP 381: Remove the Solaris and SPARC Ports
JEP 383: Foreign-Memory Access API (Second Incubator)
JEP 384: Records (Second Preview)
JEP 385: Deprecate RMI Activation for Removal Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

66
Sealed classes 21

67
Java 16
JSR 391 (March 2021)

68
Make the JDK developers life easier

JEP 338: Vector API (Incubator)


JEP 347: Enable C++14 Language Features
JEP 357: Migrate from Mercurial to Git
JEP 369: Migrate to GitHub
JEP 376: ZGC: Concurrent Thread-Stack Processing
JEP 380: Unix-Domain Socket Channels
JEP 386: Alpine Linux Port
JEP 387: Elastic Metaspace
JEP 388: Windows/AArch64 Port
JEP 389: Foreign Linker API (Incubator)
JEP 390: Warnings for Value-Based Classes
JEP 392: Packaging Tool
JEP 393: Foreign-Memory Access API (Third Incubator)
JEP 394: Pattern Matching for instanceof Deprecation
JEP 395: Records Garbage Collector
JEP 396: Strongly Encapsulate JDK Internals by Default Encapsulation
JEP 397: Sealed Classes (Second Preview) Developer efficiency
Performance

69
Strongly encapsulate JDK internals by default

--illegal-access=permit è Open to unnamed modules. 1 warning on the console

--illegal-access=warn è Same. But tons of warnings

--illegal-access=debug è Same, but stack trace of the warnings

--illegal-access=deny è Forbidden unless –add-opens is used

This flag is now deprecated.

70
Value-based Classes 25

71
5 minutes break

72
Java 17
JSR 392 (September 2021)

73
LTS!!!!

JEP 306: Restore Always-Strict Floating-Point Semantics


JEP 356: Enhanced Pseudo-Random Number Generators
JEP 382: New macOS Rendering Pipeline
JEP 391: macOS/AArch64 Port
JEP 398: Deprecate the Applet API for Removal
JEP 403: Strongly Encapsulate JDK Internals
JEP 406: Pattern Matching for switch (Preview)
JEP 407: Remove RMI Activation
JEP 409: Sealed Classes
JEP 410: Remove the Experimental AOT and JIT Compiler
JEP 411: Deprecate the Security Manager for Removal
JEP 412: Foreign Function & Memory API (Incubator)
JEP 414: Vector API (Second Incubator)
JEP 415: Context-Specific Deserialization Filters Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

74
Pattern Matching for switch 27

75
Java 18
JSR 393 (March 2022)

76
UTF-8 for all

JEP 400: UTF-8 by Default


JEP 408: Simple Web Server
JEP 413: Code Snippets in Java API Documentation
JEP 416: Reimplement Core Reflection with Method Handles
JEP 417: Vector API (Third Incubator)
JEP 418: Internet-Address Resolution SPI
JEP 419: Foreign Function & Memory API (Second Incubator)
JEP 420: Pattern Matching for switch (Second Preview)
JEP 421: Deprecate Finalization for Removal

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

77
UTF-8 by default 30

78
Simple Web Server 31

79
Code Snippets in Java API Documentation 32

80
Java 19
JSR 394 (September 2022)

81
Virtual Threads

JEP 405: Record Patterns (Preview)


JEP 422: Linux/RISC-V Port
JEP 424: Foreign Function & Memory API (Preview)
JEP 425: Virtual Threads (Preview)
JEP 426: Vector API (Fourth Incubator)
JEP 427: Pattern Matching for switch (Third Preview)
JEP 428: Structured Concurrency (Incubator)

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

82
Record Patterns 33

83
Virtual Threads 34

84
Structured Concurrency 35

85
Java 20
JSR 395 (March 2023)

86
Cool concurrency

JEP 429: Scoped Values (Incubator)


JEP 432: Record Patterns (Second Preview)
JEP 433: Pattern Matching for switch (Fourth Preview)
JEP 434: Foreign Function & Memory API (Second Preview)
JEP 436: Virtual Threads (Second Preview)
JEP 437: Structured Concurrency (Second Incubator)
JEP 438: Vector API (Fifth Incubator)

Deprecation
Garbage Collector
Encapsulation
Developer efficiency
Performance

87
Scoped Values 36

88
Java 21
JSR 396 (September 2023)

89
LTS!!!!

JEP 430: String Templates (Preview)


JEP 431: Sequenced Collections
JEP 439: Generational ZGC
JEP 440: Record Patterns
JEP 441: Pattern Matching for switch
JEP 442: Foreign Function & Memory API (Third Preview)
JEP 443: Unnamed Patterns and Variables (Preview)
JEP 444: Virtual Threads
JEP 445: Unnamed Classes and Instance Main Methods (Preview)
JEP 446: Scoped Values (Preview)
JEP 448: Vector API (Sixth Incubator)
JEP 449: Deprecate the Windows 32-bit x86 Port for Removal
JEP 451: Prepare to Disallow the Dynamic Loading of Agents
JEP 452: Key Encapsulation Mechanism API Deprecation
JEP 453: Structured Concurrency (Preview) Garbage Collector
Encapsulation
Developer efficiency
Performance

90
String Templates 37

91
Unnamed Patterns and Variables 38

92
Unnamed Classes and Instance Main Methods - Java21

93
Sequenced Collections 36

94
Conclusion

95
Who has learned
something today?

?
96
Links
Java Champions – Java is still free
https://medium.com/@javachampions/java-is-still-free-3-0-0-ocrt-2021-bca75c88d23b

Inside Java
https://inside.java/

Dev Java
https://dev.java/

Virtual threads
https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html

Var styleguide
http://openjdk.java.net/projects/amber/LVTIstyle.html

Source code
https://github.com/henri-tremblay/java91011beyond

JDK
https://openjdk.java.net/projects/jdk/
https://docs.oracle.com/en/java/javase/

Sdkman
https://sdkman.io/

97
Other courses

Mailing list
https://javaspecialists.eu/courses/oreilly/

Courses by
• Cay Horstmann
• Eric Freeman and Elisabeth Robson
• Heinz Kabutz. Including self-paces ones like
• Learn reflection with Java https://www.oreilly.com/videos/learn-reflection-with/9780138314767/
• Ken Kousen
• Kirk Pepperdine (Memory Efficient Java on July 15)
• Maurice Naftalin (Java Threads Demystified on July 15)

98
http://objenesis.org

Questions? http://montreal-jug.org

Henri Tremblay
Blog: https://blog.tremblay.pro
GitHub: https://github.com/henri-tremblay
?
LinkedIn: https://www.linkedin.com/in/henritremblay/
X: https://twitter.com/henri_tremblay
Mastodon: https://mastodon.world/@henri http://easymock.org
99

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy