Vivid java 8-evolution of java 8

Source: Internet
Author: User

I recently learned new features of Java 8. First, install Java 8. Java 8 is easy to install in windows 7, but windows XP Java 8 is not officially supported. If you install Windows xp, the following error is reported:

Unable to locate the program Input Point RegDeleteKeyExA in the dynamic link library ADVAPI32.dll



If you want to use a 32-bit XP system, you can first download the jdk exe file of Java 8. Decompress the package using 7zip. Decompress the tools.zip file. Finally, you can get the following directory structure:


Run the following command in this directory:

FOR/R % f IN (*. pack) DO "d: \ java8 \ bin \ unpack200.exe"-r-v "% f" "% ~ Pf % ~ Nf. jar"

Note d: \ java8 \ bin \ unpack200.exe. This is the unpack200.exe in your example. The following command uses unpack200.exe to convert the. pack file in the decompressed path to the. jar file. After the conversion, the path d: \ java8 is already the JAVA_HOME path.

The following describes how IDE can use the new features of java1.8. Download a new version of eclipse. In addition, for eclipse kepler, you can download a java8 plug-in "eclipse Java Development Tools Patch with Java 8 support (for Kepler SR2)" In Eclipse market. In the tab, choose windows> Pererfence> java> Installed JREs => "add .." => Standard VM => JRE home and select the decompression path. Eclipse will help you fill in the remaining content. Click OK.


Select execute Environments, javaSE-1.8, Java8, and click OK.


Create a project. Then, select Java Compiler version 1.8.


Create an interface. Write the following code. If no error is reported by IDE. So eclipse IDE can use the new features of Java 8.

public interface TestDefault {    default void pirnt(){        System.out.println();    }}
There is no compilation error in the following Pipeline


For Java 8, 55 new features are introduced online. But in details, the most useful is just a few. Below is a list:

1. Eliminate the permanent generation. There is no such error as OutOfMemoryError. PermGen Space. The persistent generation (Interned string, class metadata and class static variables) is merged into Heap Space or direct buffer. In the future, there may be more OutOfMemoryError: Heap space.

2. More powerful Annotation. You can add a tag such as @ notnull to the function parameters to determine whether the parameter is null. In addition, it can be used to eliminate public and private keywords.

3. In terms of encryption, AES uses CPU commands to facilitate encryption and decryption. SHA-224 instead of SHA-1. Supports 64-bit windows PKCS #11, encryption and decryption AES, RSA supports more PKCS.

4. Add the JS engine. You can run js scripts. This is of greater significance to the use of node. js and webview.

5. Http URLs Support Http GET and POST operations. Is this a replacement for HttpClient in the future?

6. Use the Balance Tree to handle conflicts in HashMap. For frequent conflicts, the List will be replaced with the Balance Tree. This will increase the insert speed of HashMap.

7. Base64 can be used properly. Java. util. Base64.Encoder and java. util. Base64.Decoder. Do not use sun. misc. BASE64Encoder.

8. Concurrency support. Add atomic variables suitable for frequent updates but not frequent reads. ConcurrentHashMap update and Fork-Join framework update. Arrays. sort has concurrent sort => Arrays. parallelSort, which can well use multi-core resources. It is said that the performance has been improved by at least 30% in the case of data balancing.

9. Introduce the lamda expression, default function in the interface and stream


For the above points, we will talk about points 8th and 9th. A small experiment was conducted at to verify that the sorting performance is improved in the case of concurrency:

Public class TestJava8 {public static void main (String [] args) {int count = 10000000; int [] k = new int [count]; Random r = new Random (); for (int j = 0; j <5; j ++) {for (int I = 0; I
 
  

Run on a pseudo-dual-core machine in the company. The gc () effect may also be mixed several times. The result is basically as follows:

1398:parallel:1373:serial:1224:parallel:1287:serial:1261:parallel:1274:serial:1304:parallel:1314:serial:1234:parallel:1480:serial:


In terms of results, concurrency still needs to be used when the data volume is large. However, whether memory sorting can be used for large data volumes is still a problem. For parallelSort, Java 8 still uses the Fork-Join mode. The effect on small data volumes is not good because it takes some time to start the thread. I personally think that Java 8 is better at making concurrent/non-concurrent judgments for the Arrays. sort () interface. After all, if multiple cores are hidden from java programmers, it is best to hide concurrent sort () to java programmers. Otherwise, you need to determine the data size at the upper layer, and then execute sort () or parallelSort (). In addition, the official data balancing process increases by at least 30%. This means that the official test runs out of a balanced dataset, but the dataset in the production environment is not balanced.


The biggest highlight of Java 8 is the lamda expression. You can use java for functional programming. What are the benefits?

1. If the lamda expression only performs read-only operations on variables, it must be threadSafe. In non-read-only situations, it is necessary to determine whether it is threadSafe.

2. threadSafe can be executed concurrently.

3. The results from the introduction of parameters are for API programming. (This is somewhat false)

4. The returned result can also be a function.


Write a piece of code as follows:

// Calculate the maximum value of public static void main (String [] args) in a List {List
   
    
K = new ArrayList
    
     
(); K. add (1); k. add (2); k. add (3); k. add (8); k. add (11); k. add (20); k. add (50); int max = Integer. MIN_VALUE; for (int j: k) {if (max <j) {max = j ;}} System. out. println (max );}
    
   


If finding the maximum value in this Code becomes a performance bottleneck, you need to change this code to concurrent operations. It is not easy to think about it. Now use the lamda expression to write:

// Calculate the maximum value of public static void main (String [] args) in a linked List {List
   
    
K = new ArrayList
    
     
(); K. add (1); k. add (2); k. add (3); k. add (8); k. add (11); k. add (20); k. add (50); Optional
     
      
Max = k. stream (). max (a, B)-> {return a-B;}); System. out. println (max );}
     
    
   


To change to concurrent operations, write as follows:

// Calculate the maximum value of public static void main (String [] args) in a linked List {List
   
    
K = new ArrayList
    
     
(); K. add (1); k. add (2); k. add (3); k. add (8); k. add (11); k. add (20); k. add (50); Optional
     
      
Max = k. parallelStream (). max (a, B)-> {return a-B;}); System. out. println (max );}
     
    
   


It's easy. Change k. stream () to k. parallelStream ()


This is simply based on the following points.

1. Write ThreadSafe functions or lamda expressions, which can easily be changed to multi-threaded operations.

2. Stream processing data.

3. The input parameter data is only related to the result of the previous step, but not the intermediate data generated by other data operations in this step.

4. Fork-Join provides a good multi-threaded framework.


Let's gossip about why Java 8 has done some things besides lamda expressions.

1. The default function interface in the interface. The reason is that the lamda expression is made for concurrency. Therefore, the concurrent package already has a lot of interfaces, hoping to be compatible with the previous version to reduce the migration workload. If an interface is added to the interface, the code before JDK 8 is compiled incorrectly. To solve this compilation error, you must either abstract an abstract class implementation interface and add the default implementation. You can also add this interface to all previous implementations. So I think this migration is a huge workload. Then the default function is born. In order not to write abstract class, and to use jdk5, jdk6, jdk7 code, you can easily migrate to jdk8. The default function is used as follows:

Public class TestF {public interface TestDefault {default void print () {System. out. println ("ew2we") ;}} public static void main (String [] args) {// write an anonymous class new TestDefault (){}. print ();}}


It is worth noting that if two interfaces implement the default function with the same name, an error will be reported during compilation.


2. SAM (single abstract method) is the Annotation @ FunctionalInterface. If this Annotation is included, this interface only has one unimplemented method. Of course, there can be multiple implemented default methods. This is done for the lamda expression. The lamda expression is like: (param list)-> {code statements;}, which can basically be considered as a function. Therefore, it is required that only one function method is not implemented to make the Code look good. @ FunctionalInterface:

@ FunctionalInterfacepublic interface DefaultInterface {default void hello () {System. out. println ("hello");} default void zzz () {System. out. println ("zzz");} void z (); // only one unimplemented function}



3. Stream is used to better use the Fork-Join framework. Stream is pipeline. For APIs that only care about input and output. Writing Stream code is very easy to implement concurrency. In fact, it is to throw a group of similar data into a pool, and then the thread pool starts to do things for the data. Before the execution is complete, join all the threads to put the final result into the final returned pool and return it. However, the simplest stream operation is convenient calling. It can be understood as a chain call. However, for the Style of code writing, try to write each call in different lines, which will facilitate debugging. The following is an example of Stream:

Public static void main (String [] args) {List
   
    
K = new ArrayList
    
     
(); K. add (1); k. add (2); k. add (3); k. add (8); k. add (11); k. add (20); k. add (50); // find the three numbers in the list greater than 2, and print them out k. parallelStream (). filter (a)-> {return a> 2 ;}). unordered (). limit (3 ). forEach (a)-> {System. out. println (a) ;}); // obtain the maximum value of the list element greater than 1. If no element in the list is greater than 1, Optional. emptyOptional is returned.
     
      
Max = k. parallelStream (). filter (a)-> {return a> 1 ;}). max (a, B)-> {return a-B;}); System. out. println (max );}
     
    
   


Here is a brief introduction to the syntax in lamda expressions. This lamda expression implements an abstract method. Therefore, you need to pay attention to whether the abstract method returns void, which is related to whether your expression contains return. the syntax of lamda is the parameter list. When there are 0 or more parameters, parentheses must be used. If there is only one parameter, parentheses can be omitted. Then there is an arrow->. Next is the function body. The function body must be enclosed by curly brackets. If there is only one statement, the curly brackets can be omitted. The following lamda expressions are correct.

a -> return a+1;(a) -> return a+1;(a) -> {return a+1;}(a,b) -> {int c = a+b; return c>>1;}()->System.out.println("empty");


Note the final variable and this pointer.

Import java. util. arrayList; import java. util. arrays; import java. util. list; import java. util. optional; public class TestLamba {public static class Score {int score; public List
   
    
FindLarger (List
    
     
Scores) {List
     
      
Tmp = new ArrayList
      
       
(); // Note that this is the thisscores of the Score Object. stream (). filter (a)-> {return a> this. score ;}). forEach (a)-> {tmp. add (a) ;}); return tmp;} public static List
       
        
FindLarger (List
        
          Scores, Score myScore) {List
         
           Tmp = new ArrayList
          
            (); // MyScore = new Score (); compile wrong // here, myScore is semantic final, but the domain in myScore can be changed // myScore. score = 3; this is correct and is also correct in the lamda expression. // For myScore, the lamda expression is not threadSafescores. stream (). filter (a)-> {return a> myScore. score ;}). forEach (a)-> {tmp. add (a) ;}); return tmp ;}} public static void main (String [] args) {Arrays. asList (). forEach (a)-> System. out. println ("I'm" + a); List
           
             K = new ArrayList
            
              (); K. add (1); k. add (2); k. add (3); k. add (8); k. add (11); k. add (20); k. add (50); Score s = new Score (); s. score = 11; s. findLarger (k ). forEach (a-> {System. out. println (a) ;}); System. out. println ("dsdsd"); Score. findLarger (k, s ). forEach (a-> {System. out. println ();});}}
            
           
          
         
        
       
      
     
    
   


There is no final variable in the parameter. In terms of syntax, Java 8 does not require that the external reference parameter be final, but the semantics is still final. This is similar to the anonymous internal class. In addition, this pointer must be a pointer to the outer class. The lamda expression does not have this.


Now java jdk may easily use multi-core resources. Therefore, it is expected that jdk In The Future of java will be able to use resources of multiple machines, such as map/reduce. This will make it easier to write programs. It looks like this trend ~~~ Continue to evolve ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.