1

In this Java 18, a new @snipppet tag has been added, which is mainly used for scenarios where sample code needs to be placed in JavaDoc. In fact, before Java 18, there was already a @code tag, which can be used to write small pieces of code in JavaDoc. For example, in the previous video about Simple Web Server , the sample code mentioned is Written with the @code tag:

 * <p> Example of a simple file server:
 * <pre>{@code
 *    var addr = new InetSocketAddress(8080);
 *    var server = SimpleFileServer.createFileServer(addr, Path.of("/some/path"), OutputLevel.INFO);
 *    server.start();
 * }</pre>

Although the previous version has similar functions, there are still various shortcomings, so this time the @snipppet tag has been enhanced in these aspects:

  • Validity check, for example: when our sample code contains some syntax errors, an error message will appear

  • Syntax highlighting, as shown in the figure below, when using @snippet , IDE can already support highlighting, which is more conducive to editing. In contrast, the content of @code is not so good when editing. comfortable

  • IDE support for code editing has also been greatly improved. In the past, when using @code to edit code, it was basically edited in plain text, but now when using @snippet , The IDE gives more and better support, such as everyone's favorite code hints:

Therefore, this function DD thinks it is still very useful, especially for some small partners who write frameworks and SDKs (after all, the requirements for JavaDoc are high).

The video corresponding to this content has been recorded, click to view: Java 18 new features: @snipppet tag

If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better! Also, don't walk away, follow me! Continue to update the new Java feature tutorial!

Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources

程序猿DD
2.2k 声望2.8k 粉丝

作品:《Spring Cloud微服务实战》、SpringForAll社区、OpenWrite、Youtube中文配音