Get the App
SLTechnology News&Howtos  ›  Development  › 

Overview and example code of method references and construction references in Java8

Shulou Source: shulou.com Published: 2022-06-03 14:53:09 10月03日 Update

This article mainly explains the "Java8 method reference and construction reference overview and example code", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "method reference and construction reference overview and example code in Java8"!

I. Overview of method references

A method reference is an abbreviation for a specific Lamda expression. The idea is to call the function directly using the method name if you can replace the Lamda expression. Its syntax format: class name:: method name.

Two and three methods are quoted.

1 A reference to a static method

Syntax format: static class name (ClassName):: method name (MethodName)

Example:

/ / 1 Lamda static method @ Test public void LamdaSTest () {String youku1327 = "1327"; Function function = s-> ObjectUtils.allNotNull (youku1327); System.out.println (function.apply (youku1327)); / / true} / / static method reference @ Test public void MethodRefTest () {String youku1327 = "youku1327"; Function function = ObjectUtils::allNotNull; System.out.println (function.apply (youku1327)); / / true}

2 references to methods that point to the object instance

Syntax format: instance name (instanceName):: method name (MethodName)

This object refers to an external object that is not an input parameter

Example:

/ / 2 Lamda expression @ Test public void ObjectLamdaTest () {Car car = new Car, "black", "China", 20); Supplier supplier = ()-> car.getColor (); System.out.println (supplier.get ()); / / black} / / object reference @ Test public void ObjectRefTest () {Car car = new Car ("100s", "black", "China", 20); Supplier supplier = car::getColor; System.out.println (supplier.get ()) / / black}

3 method reference pointing to the instance

Syntax format: object name (ClassName):: method name (Method) this object refers to the input parameter object

/ / 3 Lamda expression @ Test public void InstanceMethodLamdaTest () {Car car = new Car ("100s", "black", "China", 20); Function function = s-> s.getColor (); System.out.println (function.apply (car)); / / black} @ Test public void InstanceMethodRefTest () {Car car = new Car ("100s", "black", "China", 20); Function function = Car::getColor; System.out.println (function.apply (car)); / / black}

III. Constructor reference

Syntax format: object name (ClassName):: new

@ Test public void constructLamdaTest () {BiFunction biFunction = (youku1327 aDouble)-> new Car; Car car = biFunction.apply ("youku1327", 50.0); / / Car (code=youku1327, color=null, factory=null, price=50.0) System.out.println (car);} @ Test public void construcMethodRefTest () {BiFunction biFunction = Car::new; Car car = biFunction.apply ("youku1327", 50.0); / / Car (code=youku1327, color=null, factory=null, price=50.0) System.out.println (car);}

Thank you for your reading, the above is the "Java8 method reference and construction reference overview and example code" content, after the study of this article, I believe you on the Java8 method reference and construction reference overview and example code of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Methods objects instances names formats syntax code expressions static Chinese pointing learning content that is ideas examples class names methods of use functions situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info NVidia OPPO Reno Xiaomi