Everlasting standard compiler optimizations
☕ Do not require any flag.
☕ Literal constants are folded.
☕ String concatenation is folded.
☕ Constant fields are inlined.
☕ Dead code branches are eliminated.
public class Example {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("foo");
System.err.println("" + builder + builder.append("bar"));
}
}
// before JDK 19 prints foobarfoobar
// strategy after JDK 19:prints foofoobar
public class Example {
public static void main(String[] args) {
long b = 5L;
b += 0.1 * 3L;
}
}
# After JDK 20
$ javac Example.java -Xlint:all
> Example.java:4: warning: [lossy-conversions] implicit cast from double to long in compound assignment is possibly lossy
> b += 0.1 * 3L;
^
> 1 warning
Goals for API documentation
☕ Helps with product maintenance.
☕ Technical users can understand your APIs goals.
☕ Can increase awareness/adoption of your software.
☕ Third-party developers can start quickly by trying out API examples.
Inserting fragments of source code in documentation
☕ Wrap code examples inside <pre> and {@code ...}.
☕ Automatically escape special characters with {@code ...}.
☕ No control over indentation.
☕ No code highlighting.
Elegant inclusion of code examples
☕ JEP 413 introduced {@snippet ...} tag in JDK 18.
☕ A better presentation of the targeted code examples via regions.
☕ Control code via @highlight, @replace, @link tags and regions.
Elegant inclusion of code examples
☕ JEP 413 introduced {@snippet ...} tag in JDK 18.
☕ A better presentation of the targeted code examples via regions.
☕ Control code via @highlight, @replace, @link tags and regions.
☕ The tag accepts separate files that contain the content of the snippet.
/**
* <p>Java class for user complex type.
* </p>
* <p>The following schema fragment specifies the expected content for the User class.
* </p>
* {@snippet file="user.xml" lang=xml}
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {"id", "email", "firstName", "lastName","userName”})
public class User {
Markdown in documentation comments
☕ JEP 467 introduces Markdown in Java documentation comments.
☕ Enjoy a concise syntax for common constructs.
☕ Reduce the dependence on HTML markup and JavaDoc tags.
☕ Keep enabling the use of specialized tags when not available in Markdown.
☕ A new form of documentation comment: ///
Fast prototyping java code with jshell
☕ Quickly try, debug and learn Java and its APIs.
☕ Experiment with Java by bypassing the compile stage.
☕ Get immediate feedback on your code.
☕ Exposes a programmatic API.
jshell> var check = new Boolean("true");
| Warning:
| Boolean(java.lang.String) in java.lang.Boolean has been deprecated and marked for removal
| var check = new Boolean("true");
| ^-----------------^
check ==> true
jshell> Short number = new Short("12345");
| Warning:
| Short(java.lang.String) in java.lang.Short has been deprecated and marked for removal
| Short number = new Short("12345");
| ^----------------^
number ==> 12345
JDK tools access in jshell
☕ jshell supports loading scripts.
☕ Scripts can be local files or one of the predefined scripts.
☕ Scripts may hold any valid code snippets or jshell commands.
# Web development testing, to simulate locally a client-server setup.
$ jwebserver
> Binding to loopback by default. For all interfaces use "-b 0.0.0.0" > or "-b ::".
> Serving /cwd and subdirectories on 127.0.0.1 port 8000
> URL: http://127.0.0.1:8000/
# Use static files as API stubs for web-service prototyping or application testing.
$ jwebserver -p 9000
> Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
> Serving /example and subdirectories on 127.0.0.1 port 9000
> URL http://127.0.0.1:9000/
> 127.0.0.1 -- [09/Feb/2024:3:02:05 +0200] "GET /api/a1.json HTTP/1.1" 200
> 127.0.0.1 -- [09/Feb/2024:3:02:06 +0200] "GET /api/a2.json HTTP/1.1" 200
# Search a directory on a remote server from your local machine.
$ jwebserver -b 0.0.0.0
> Serving /work and subdirectories on 0.0.0.0 (all interfaces) port 8000
> URL http://192.168.178.41:8000/
# Can also be started via java launcher
$ java -m jdk.httpserver
> Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
> Serving /example and subdirectories on 127.0.0.1 port 8000
> URL http://127.0.0.1:8000/
# Since JDK 18, you can check the version of keytool and jarsigner.
$ keytool -help -version
> keytool -version [OPTION]...
> Prints the program version
> Options:
> Use "keytool -?, -h, or --help" for this help message
> Generated 128-bit ARCFOUR secret key [Storing example.p12]
> Warning: The generated secret key uses the ARCFOUR algorithm which is considered a security risk.
New jfr view command
☕ Displays aggregated event data without the need to dump a recording file.
☕Start JFR a recording via -XX:StartFlightRecording or jcmd.
$ java -XX:StartFlightRecording -jar imaging.jar
☕ Use the PID or jar name in the command.
☕ Use jps JDK tool to list all running Java processes (PID).
Useful links
☕ Programmer’s Guide to Snippets: https://docs.oracle.com/en/java/javase/21/javadoc/programmers-guide-snippets.html
☕ Java 21 Tool Enhancements: Better Across the Board #RoadTo21 https://www.youtube.com/embed/nFJBVuaIsRg
☕ Using Jshell API https://inside.java/2022/11/21/jshell-java-source-browser/
☕ Christian Stein’s article on jshell tooling https://sormuras.github.io/blog/2023-03-09-jshell-tooling
☕ Julia Boes article on more advanced jwebserver usage https://inside.java/2021/12/06/working-with-the-simple-web-server/
☕ JEP 408 on jwebserver https://openjdk.org/jeps/408
☕ More on jpackage: https://dev.java/learn/jvm/tool/jpackage/
☕ Package a JavaFX application as a native executable https://inside.java/2023/11/14/package-javafx-native-exec/
☕ Stack Walker ep 2 on JFR https://inside.java/2023/05/14/stackwalker-02/
☕ Monitoring Java Application Security with JDK tools and JFR Events: https://dev.java/learn/security/monitor/
☕JEP 467 on markdown documentation cmments: https://openjdk.org/jeps/467