What is a first class citizen function?

后端 未结 7 1923
清酒与你
清酒与你 2020-12-02 09:05

What is a first class citizen function?

Does Java supports first class citizen function?

Edit:
As mention on Wikepedia

F

7条回答
  •  佛祖请我去吃肉
    2020-12-02 09:54

    Let us consider the example of functional programming paradigm in which functions are the first class citizens. When we say functions are the first class citizens, we can do the following things with the function...

    • Function can be assigned to a variable
    • Function can be stored in a data structure
    • Function can be passed around as an argument to other functions
    • Function can be returned from the functions

    In functional programming languages, it is possible to do the above mentioned things.

    Now, let us try to answer the question, whether java supports first class citizen functions (or) not.

    In java, methods are equivalent of functions. It is not possible to do any of the above with methods. But all of the above are possible with java objects. So, objects are the first class citizens in java. Admittedly, java8 supports passing of methods (method behavior, to be precise) to other methods using functional interfaces and lambda expressions. But that does not mean that java has functions as first class citizens.

    The ability to do above things such as passing around functions, returning functions from functions is very powerful and useful. This is because, it allows us to pass around the behavior not just the data.

提交回复
热议问题