Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand java1.8 deeply and know supplier

Shulou Source: shulou.com Published: 2022-06-02 02:05:48 09月25日 Update

This article shows you how to deeply understand java1.8 and know supplier. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Supplier is also used to create objects, but unlike the traditional syntax for creating objects: new, see the following code:

Public class TestSupplier {private int age

TestSupplier () {System.out.println (age)

} public static void main (String [] args) {

/ / create a Supplier container, declared as TestSupplier, and the object constructor will not be called, that is, the object Supplier sup= TestSupplier::new will not be created.

System.out.println ("-")

/ / when the get () method is called, the constructor of the object is called, and the real object sup.get () is obtained.

/ / each time get calls the constructor, that is, the object obtained is different sup.get ()

}}

Output result:

-00

Official code and comments:

/ * Represents a supplier of results. *

*

There is no requirement that a new or distinct result be returned each * time the supplier is invoked. *

*

This is a functional interface *

Whose functional method is {@ link # get ()}.

* * @ param the type of results supplied by this supplier

* * @ since 1.8 * / @ FunctionalInterfacepublic interface Supplier {

/ * Gets a result. *

* @ return a result * / T get ()

}

According to the code and official comments, my personal understanding is:

1.supplier is an interface with a get () method

two。 Syntax:

Supplier sup= TestSupplier::new

3. Each time the get () method is called, the constructor is called to create a new object.

The above content is how to deeply understand java1.8 and know supplier. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Objects methods code content official skills comments knowledge syntax different concise concise personal traditional container that is interfaces articles more articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker Shulou Tech Info macOS vpn