java9 new features


1. Directory structure 

2.repl tool jShell command 

3. Modular 

4. Multi-version compatible jar package 

5. Improvement of interface methods (jdk7 can only declare full-name constants and abstract methods in the interface; jdk8 adds static methods and default methods; jdk9 adds private methods)

6. The diamond operator upgrade can have {} 

7. Exception handling try upgrade 

8. Underscore identifier naming restrictions _ not allowed 

9. Changes in the underlying storage structure of String (char arrays become byte arrays) 

10. Enhanced Stream API (4 new methods are added on the basis of java8) 

11. Introduce httpClient api in jdk9 to replace the original HttpURLConnection


1. One of the new features of Java9 - directory structure


Including jdk8 and previous jdk versions, all directory structures and directory meanings are shown in the figure:


640?wx_fmt=png&wxfrom=5&wx_lazy=1&retryload=1 

640?wx_fmt=png


    After jdk9, the directory structure changes as shown in the figure:


    640?wx_fmt=png


This new feature only needs to be understood. This directory structure is convenient for guaranteeing the next new feature.


2. One of the new features of Java9 - JShell tool


How to understand and how to use it? This is just for java9, which is equivalent to the cmd tool. You can write methods directly like cmd, etc., but I think it is only suitable for beginners to do some of the simplest operations and write some methods:

Open this tool in cmd:

        640?wx_fmt=png


After entering the tool, you can do some simple java operations

        640?wx_fmt=png


wait, i think it's only for beginners to learn java you can learn java without other editing tools


3. One of the new features of Java9 - modularization


 A large-scale project, such as Taobao Mall, etc., will contain multiple modules, such as order module, front-end module, background management module, advertising space module, membership module...etc. Each module will call each other, but this kind of There will be very few cases, only for special cases, if a project has 30 module systems for development, but as long as a single module runs, all modules will be driven, so the memory and performance for JVM will be very low. , so, java9 provides this feature. When a certain module is running, the JVM will only start the modules that it depends on, and will not load all the modules into the memory, which greatly improves the performance. Written as follows:

        640?wx_fmt=png


Two modules in a project, the modules are associated with module-info.java, right-click in the IDEA editor to create package-info.java


        640?wx_fmt=png


In the two modules java9Demo and java9Test, java9demo writes an entity class Person, and calls such a process in java9Test


This is java9Demo. Exports the files required by the java9Test module and exports the package where it is located. 


640?wx_fmt=png


Then create a package-info.java in the java9Test module, import the java9Demo module to export the package name


640?wx_fmt=png


In this way, the Person entity class can be introduced directly in java9Test, which is just a simple example. exports controls which packages can be accessed by modules, so unexported packages cannot be accessed by other modules


4. One of the new features of Java9 - multi-version compatible Jar package


How to understand it?


Most of the jdk used by many companies are still old versions, including jdk6 and 7. They dare not upgrade mainly because of compatibility issues, but java9 has achieved this, that is, no matter whether the company's project uses java6, 7, 8 or even 5. He can be compatible without error. For example, you used iphone5 before, and now there are iPhone6, iphone7, iphon8 and iphone9, but you dare not buy 9, because you have adapted to the operation of all mobile phones of iphone5. The process, 6, 7, 8, the running process of each mobile phone is different, but this 9 is very powerful, it can recognize that the version iphone you are using is 5, so when you upgrade to iphone9, the running process of your mobile phone is still the same. The process of iphone5 only has all the advantages of iphone9 on the original basis.


5. One of the new features of Java9 - the upgrade of the interface


640?wx_fmt=png


6. One of the new features of Java9 - the upgrade of the diamond operator


640?wx_fmt=png


7. One of the new features of Java9 - exception handling try upgrade


First look at the comparison of try catch of jdk6,7,8,9


Java6 processing method:


640?wx_fmt=png


Java7,8 common processing method:


640?wx_fmt=png


 The processing method of java9:


640?wx_fmt=png


It should be clear by now.


8. One of the new features of Java9 - special identifiers increase restrictions


Before java8, String _ = "hello"; Such identifiers can be used, but not in java9.


9. One of the new features of Java9 - String underlying storage structure replacement


Before java8, the underlying structure type of String was char[], but java9 replaced it with byte[]. In this way, it saves space and improves performance.


    640?wx_fmt=png

    640?wx_fmt=png


The reason for the replacement is because the smallest unit has always been a char and two bytes are used, but java8 is based on latin1, and this latin1 code can be identified by a byte, so when your data can clearly use a byte, We used two bytes, a minimum unit of chat, and there is one more byte of space. So java9 has been updated in this regard. Now java9 is based on ISO/latin1/Utf-16, latin1 and ISO are identified by one byte, UTF-16 is identified by two bytes, java9 will automatically identify which encoding to use, when the data When 1byte is used, iSO or latin1 will be used. When the spatial data meets 2byte, utf-16 will be used automatically, which saves a lot of space.


    640?wx_fmt=png


Similarly, StringBuilder StringBuffer also replaces the underlying data structure


10. One of the new features of Java9 - the addition of new methods of Stream API


Added 4 new methods to the original Stream API, takeWhile dropWhile ofNullable iterate (new overloaded method)


First explain that takeWhile ends when a certain condition is reached: the output result is 45 43, as shown in the figure


    640?wx_fmt=png


while dropWhile is the opposite of takeWhile


ofNullable, in java8 the element in Stream cannot be completely null, otherwise null pointer exception, and in the upgrade of java9, it is allowed to create null


iterate unconditional infinite loop


    640?wx_fmt=png


11. One of the new features of Java9 - the introduction of HttpClient


In the past, we have added httpclient through maven, and java9 can be imported directly

 

All features of Java9 are designed to improve performance and memory.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326986902&siteId=291194637