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 new features of Java 8

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what are the new features of Java 8", friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of Java 8.

Introduction

Java 8 (also known as jdk 1.8) is a major version of the Java language development. Oracle released Java 8 on March 18, 2014, which supports functional programming, new JavaScript engine, new date API, new Stream API, etc.

New features

A lot of new features have been added to Java8, and we focus on the following:

The 1.Lambda expression − Lambda allows you to take a function as an argument to a method (the function is passed as an argument to the method).

The syntax format is as follows:

(parameters)-> expression

Or

(parameters)-> {statements;}

Important features of lambda expressions:

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

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

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

d. 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.

Example:

/ / 1. No parameter is required, and the return value is 5

()-> 5

/ / 2. Receives a parameter (numeric type) and returns twice its value

X-> 2 * x

/ / 3. Accept 2 parameters (numbers) and return their difference

(X, y)-> x-y

/ / 4. Receives two int integers and returns their sum

(int x, int y)-> x + y

/ / 5. Accept a string object and print it on the console without returning any value (looks like returning void)

(String s)-> System.out.print (s)

two。 Method references − method references provide useful syntax for directly referencing methods or constructors of existing Java classes or objects (instances). Used in conjunction with lambda, method references can make the construction of the language more compact and less redundant code.

Method references use a pair of colons:

The differences are introduced as follows:

Constructor reference: its syntax is Class::new

Static method reference: its syntax is Class::static_method

Method reference to any object of a particular class: its syntax is Class::method

Method reference to a specific object: its syntax is instance::method

3. The default method − is a method that has an implementation in the interface.

Simply precede the method name with a default keyword to implement the default method

4. New tools − new compilation tools, such as: Nashorn engine jjs, class dependency analyzer jdeps.

The new Stream API (java.util.stream) added by 5.Stream API − introduces the true functional programming style into Java.

6.Date Time API − strengthens the handling of dates and times.

The 7.Optional class − Optional class has become part of the Java 8 class library to resolve null pointer exceptions.

8.Nashorn, JavaScript engine − Java 8 provides a new Nashorn javascript engine that allows us to run specific javascript applications on JVM.

At this point, I believe you have a deeper understanding of "what are the new features of Java 8". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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