Some new features of Java7

1. Binary numerical expression method, you can add prefix 0b or 0B to express byte, short, int and long. This corresponds to the previous hexadecimal expression 0x or 0X.

byte b = (byte)0b00100001;

int i = 0b101;

 

 

2. The middle of the value can be broken with an underscore, for example

long creditCardNumber = 1234_5678_9012_3456L;

float pi = 3.14_15F;

But underscores cannot start, end, or precede F, L

 

 

3. It can be automatically inferred when the generic type is defined, but the <> symbol is indispensable.

for example

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

can be written as:

Map<String, List<String>> myMap = new HashMap<>();

 

 

4. Try-with-resources. resource must be java.lang.AutoCloseable. Its close() method is automatically called.

 try (BufferedReader br = new BufferedReader(new FileReader(path))) {

    br.readLine();

 }

It is also possible to declare multiple resources at the same time:

try (BufferedReader br = new BufferedReader(new FileReader("path"));

    BufferedReader br2 = new BufferedReader(new FileReader("path"))) {

       br.readLine();

 }

Can also be used on JDBC Statement

try (Statement stmt = cn.createStatement()) {

    ResultSet rs = stmt.executeQuery("SELECT * FROM table1");

}

  

5. Catch multiple Exceptions at the same time

try{

 ...

} catch (IOException ex) {

 ...

}catch (SQLException ex) {

 ...

}

can write like this

try{

 ...

} catch (IOException|SQLException ex) {

 ...

}

 

6. The new file read and write package java.nio.file. Much more powerful than the previous java.io.File. Commonly used are Files, Paths

Paths: used to generate an implementation class that implements the Path interface. Due to the different platforms that jvm runs on, different Path implementation classes will be obtained.

Files: Classes that specifically operate on files, including copying files, creating directories, creating files, creating connections, deleting files, obtaining BufferReader, BufferWriter, InputStream, and OutputStream of files, reading files in the form of bytes and strings, and writing file content, etc.

 

7. The fork/join framework, based on the ForkJoinPool class, is an implementation of the Executor interface, designed to efficiently run a large number of tasks; ForkJoinTask represents a task that needs to be executed, and the threads that actually execute these tasks are placed in a thread pool ( ForkJoinPool) inside. It can divide a large task into several small tasks to execute concurrently, make full use of the available resources, and then improve the execution efficiency of the application. The work-stealing method is more efficient than ExecuteService.

 

8. The new garbage collector G1 can replace the previous CMS (Concurrent Mark-Sweep Collector)

 

Guess you like

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