Detailed explanation of new features in Java 9

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:

 

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

    

     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, 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:

        

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

        

        . . . . 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, will contain multiple modules, such as order module, front-end module, background management module, advertising space module, membership module, etc., and each module will call each other, but In this case, it will be rare, 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 for jvm, memory and performance will be reduced. Very low, so java9 provides this feature. When a certain module is running, the JVM will only start the modules that it has dependencies on, and will not load all the modules into the memory, which greatly improves the performance. Written as follows:

        

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

        

        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. 

module java9Demo{
    exports com.mdxl.layer_cj.entity;
}

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

module java9Test{
    requires java9Demo;
}

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 problems, 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, and 8 are different for each mobile phone, but this 9 is very powerful, it can recognize that the version iphone you are using is 5, so when you upgrade to iphone 9, 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 --- Interface upgrade

        

public interface FilterProcess<T> {

    //java 7 and earlier features global constants and abstract methods
    public static final String a ="22";
    boolean process(T t);

    //java 8 feature static methods and default methods
    default void love(){
        System.out.println("java8 feature default method");
    }
    static void haha(){
        System.out.println("java8 feature static method");
    }

    //java 8 features support private methods
    private void java9(){}
}

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

//java6 and before
Map<String,String> map7 = new HashMap<String,String>();
//java7 and 8 <> no data type
Map<String,String> map8 = new HashMap<>();
//java9 adds the function of anonymous inner classes and adds curly brackets {} to do some detailed operations
Map<String,String> map9 = new HashMap<>(){};

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:

//java7 and previous writing methods must be closed when each stream is opened
@Test
public void test7(){
    InputStreamReader reader = null;
    try{
        reader = new InputStreamReader(System.in);
        reader.read();
    }catch (IOException e){
        e.printStackTrace ();
    }finally {
        if (reader != null){
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
}

    Java7,8 common processing method:

//java7 and 8 and each stream must be closed when it is opened, but it is closed in the parentheses of try
@Test
public void test7(){
    try(InputStreamReader reader =new InputStreamReader(System.in)){
        reader.read();
    }catch (IOException e){
        e.printStackTrace ();
    }
}

    The processing method of java9:

//java9 and each stream must be closed when it is opened, but it is closed in the parentheses of try, in
//On the basis of java8, it is good to write variables directly in try brackets. If there are multiple streams, separate them with semicolons.
//try(reader;writer){}
@Test
public void test7(){
    InputStreamReader reader =new InputStreamReader(System.in);
    try(reader){
        reader.read();
    }catch (IOException e){
        e.printStackTrace ();
    }
}

    It should be clear by now.

8. One of the new features of Java 9---additional restrictions on special identifiers

    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.

    

    

    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.

    

*****Similarly, StringBuilder StringBuffer also replaces the underlying data structure *************

10. The addition of new methods of the Stream API, one of the new features of Java9

    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

    

    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

    

11. One of the new features of Java9---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://43.154.161.224:23101/article/api/json?id=324441009&siteId=291194637