JDK 7 new features summary example code analysis
1. Switch supports string as a parameter
Switch enumeration support
2. Improvement of digital literal quantity
2.1. Before Java 7, decimal (1234), octal (01234) and hexadecimal (0x1234) are supported
Java 8 supports binary (0b11110001)
2.2. You can use underscores to separate_
3. Resource auto close
In Java, there are many resources that need to be closed after use. Take chestnuts, InputStream, writer, sockets, connection, etc. Before Java 7, it is usually shown to call its close () method. In Java 7, you can ignore whether it is not closed. We can use the try with resources code block.
4. Catch multiple exceptions
Before Java 7, there must be multiple catch clauses to catch multiple exceptions. In Java 7, we can write as follows:
5. Instance creation type inference
6. Enhanced file system
Java 7 introduces a new nio2 0 API to change the inconvenience of file management nio. Using common types such as path, paths, files, watchservice and file system under the file package can well simplify the coding of file management by developers.
6.1 path interface and paths class
Some functions of the path interface can be compared with Java The file class under the IO package is equivalent. Of course, these functions are limited to read-only operations. In the actual development process, developers can use the path interface and the paths class to obtain a series of context information of the file.
Obtain file information by combining path interface and paths type:
6.2. Files class
Using the path interface and the paths class together can easily access the context information of the target file. Of course, these operations are all read-only. If developers want to perform other non read-only operations on files, such as file creation, modification, deletion, etc., they can use the files type. The common methods of files type are as follows:
Example of copying and pasting files using files type:
Using files type to manage files is more convenient and simple than traditional I / O. Because the specific operation implementation will be handed over to nio2 0 API, developers don't need to pay attention.
6.3. WatchService
Java 7 also provides developers with a new set of file system functions, that is, file monitoring. Many friends here may not know the significance and purpose of file monitoring, so please think back to the web container after debugging into a hot release function. When the project is iterated and redeployed, developers do not need to restart it manually, because once the web container detects that the file has changed, it will automatically adapt to these "changes" and reload internally. The hot release function of web container is also based on file monitoring function, so it must be admitted that the emergence of file monitoring function is of great significance to Java file system. File monitoring is event driven, and event triggering is a prerequisite for monitoring. Developers can use Java nio. The standardwatcheventkings type under the file package provides three literal constants to define the monitoring event type. It is worth noting that the monitoring event needs to be registered with the watchservice instance.
Monitoring events provided by the standardwatcheventkings type:
Complete example of file monitoring using watchservice class:
From the above program examples, we can see that using the watchservice interface for file monitoring is very simple and convenient. First we need to define the target monitoring path, and then call the FileSystems type newWatchService () method to create the WatchService object. Next, we need to use the register () method of the path interface to register the watchservice instance and monitor events. When all these basic operation layers are ready, we can write the peripheral real-time monitoring cycle. Finally, iterate the watchkey to obtain all the files that trigger the monitoring event.
Now I finally know the basic principle of the so-called dev tools hot update in spring boot! The original JDK provides such APIs.
summary
The above is a summary of the new features of JDK 7 introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!