Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the nine new features of Java 8?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what are the nine new features of Java 8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Java8Lambda expression

Lambda expressions, also known as closures, are the most important new feature that drives the release of Java 8. Lambda allows you to use a function as an argument to a method (the function is passed into the method as an argument). Use

Lambda expressions can make your code more compact.

1. Grammar

The syntax format of the lambda expression is as follows: (parameters)-> expression or (parameters)-> {statements;}

The following are important features of lambda expressions:

Optional type declaration: there is no need to declare parameter types, the compiler can uniformly identify parameter values.

Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.

Optional curly braces: if the body contains a statement, you do not need to use braces.

Optional return keyword: if the body has only one expression return value, the compiler will automatically return a value, and curly braces need to specify that the expression returns a value.

2. Variable scope

Lambda expressions can only refer to outer local variables marked with final, which means that local variables defined outside the domain cannot be modified within lambda, otherwise an error will be compiled.

2. Reference to the Java8 method:

A method reference points to a method by its name. Method references can make the construction of the language more compact and compact and reduce redundant code. Method references use a pair of colons:

3. Java8 functional interface

A functional interface (FunctionalInterface) is an interface that has one and only one abstract method, but can have multiple non-abstract methods.

Functional interfaces can be implicitly converted to lambda expressions.

Functional interfaces can support lambda amicably with existing functions.

Functional interfaces that have existed before JDK 1.8:

Java.lang.Runnable

Java.util.concurrent.Callable

Java.security.PrivilegedAction

Java.util.Comp

Arator

Java.io.FileFilter

Java.nio.file.PathMatcher

Java.lang.reflect.InvocationHandler

Java.beans.PropertyChangeListener

Java.awt.event.ActionListener

Javax.swing.event.ChangeListener

New function interfaces added to JDK 1.8:

Java.util.function

Java.util.function contains a number of classes to support functional programming of Java.

4. Java8 default method

Java 8 adds a default method for interfaces.

Simply put, the default method is that the interface can have an implementation method, and there is no need for the implementation class to implement its method.

We just need to precede the method name with a default keyword to implement the default method.

Why should there be this feature?

First of all, the previous interface is a double-edged sword, the advantage is that it is abstract-oriented rather than concrete-oriented programming, the defect is that when you need to modify the interface, you need to modify all the classes that implement the interface. The current java 8 collection framework does not have foreach methods, and the usual solution is to add new methods and implementations to the relevant interfaces in JDK. However, for released versions, it is not possible to add new methods to the interface without affecting the existing implementation. So the default method introduced. Their goal is to solve the problem of incompatibility between interface modifications and existing implementations.

5. Java8Stream

Java 8 API adds a new abstraction called streaming Stream, which allows you to process data in a declarative way.

Stream uses an intuitive way similar to querying data from a database with SQL statements to provide a high-level abstraction of the operations and expressions of Java collections.

Stream API can greatly improve the productivity of Java programmers, allowing programmers to write efficient, clean, concise code.

This style treats the set of elements to be processed as a stream, which is transmitted in the pipeline and can be processed on the nodes of the pipe, such as filtering, sorting, aggregation, and so on.

The element flow is processed by the intermediate operation (intermediate operation) in the pipeline, and the result of the previous processing is finally obtained by the final operation (terminal operation).

VI. Java8Optional class

The Optional class is a container object that can be null. If the value exists, the isPresent () method returns true, and a call to the get () method returns the object.

Optional is a container: it can hold the value of type T, or just null. Optional provides a lot of useful methods so that we don't have to explicitly detect null values.

The introduction of Optional class is a good solution to null pointer exceptions.

7. Java8Nashorn JavaScript

Nashorn a javascript engine.

Starting from JDK1.8, Nashorn replaces Rhino (JDK 1.6, JDK1.7) as the embedded JavaScript engine of Java. Nashorn fully supports the ECMAScript 5.1 specification and some extensions. It makes

Compiles JavaScript into Java bytecode using new JSR292-based language features, including invokedynamic introduced in JDK 7.

This results in a 2-to 10-fold performance improvement compared to previous Rhino implementations.

8. Java8 date and time API

Java8 further strengthens its handling of dates and times by releasing a new Date-Time API (JSR 310).

In the old version of Java, there are many problems with date-time API, including:

Non-thread-safe: java.util.Date is non-thread-safe, and all date classes are mutable, which is one of the biggest problems with Java date classes.

Poor design: the definition of the date / time class in Java is inconsistent, there are date classes in both java.util and java.sql packages, and the classes used for formatting and parsing are defined in the java.text package.

Java.util.Date contains both date and time, while java.sql.Date contains only date, so it doesn't make sense to include it in the java.sql package. In addition, both classes have the same name, which in itself is a very bad design.

Time zone handling is troublesome: date classes do not provide internationalization and no time zone support, so Java introduces java.util.Calendar and java.util.TimeZone classes, but they also have all the above problems.

Java8 provides a lot of new API under the java.time: package. Here are two more important API:

Local (local): simplifies the processing of date and time without the problem of time zone.

Zoned (time zone): handles date and time through a defined time zone.

The new java.time package covers all operations dealing with date, time, date / time, time zone, time (instants), process (during) and clock (clock).

IX. Java8 Base64

In Java8, Base64 coding has become the standard of the Java class library. Java 8 has a built-in encoder and decoder for Base64 coding. The Base64 utility class provides a set of static methods to obtain the following three BASE64 codecs:

Basic: the output is mapped to a set of characters A-Za-z0-9 characters /, the encoding does not add any lines, and the decoding of the output only supports A-Za-z0-9 characters /.

URL: the output maps to a set of characters A-Za-z0-9 characters, and the output is URL and file.

MIME: the output is implicitly projected to MIME-friendly format. Output no more than 76 characters per line, using'\ r 'and followed by'\ n' as a division. There is no line splitting at the end of the encoded output.

That's all for "what are the nine new features of Java 8"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report