Does Java need closures?

Question :

Does Java need closures?,

Answer :

I’ve been reading a lot lately about the next release of Java possibly supporting I feel like I have a pretty firm grasp on what closures are, but I can’t think of a solid example of how they would make an Object-Oriented language “better”. Can anyone give me a specific use-case where a closure would be needed (or even preferred)?

,

As a Lisp programmer I would wish that the Java community understands the following difference: functions as objects vs. closures.

a) functions can be named or anonymous. But they can also be objects of themselves. This allows functions to be passed around as arguments, returned from functions or stored in data structures. This means that functions are first class objects in a programming language.

Anonymous functions don’t add much to the language, they just allow you to write functions in a shorter way.

Read More  What is ‘Currying’?

b) A closure is a function plus a binding environment. Closures can be passed downwards (as parameters) or returned upwards (as return values). This allows the function to refer to variables of its environment, even if the surrounding code is no longer active.

If you have a) in some language, then the question comes up what to do about b)? There are languages that have a), but not b). In the functional programming world a) (functions) and b (functions as closures) is nowadays the norm. Smalltalk had a) (blocks are anonymous functions) for a long time, but then some dialects of Smalltalk added support for b) (blocks as closures).

You can imagine that you get a slightly different programming model, if you add functions and closures to the language.

Read More  What’s the difference between a Table Scan and a Clustered Index Scan?

From a pragmatic view, the anonymous function adds some short notation, where you pass or invoke functions. That can be a good thing.

The closure (function plus binding) allows you for example to create a function that has access to some variables (for example to a counter value). Now you can store that function in an object, access it and invoke it. The context for the function object is now not only the objects it has access to, but also the variables it has access to via bindings. This is also useful, but you can see that variable bindings vs. access to object variables now is an issue: when should be something a lexical variable (that can be accessed in a closure) and when should it be a variable of some object (a slot). When should something be a closure or an object? You can use both in the similar ways. A usual programming exercise for students learning Scheme (a Lisp dialect) is to write a simple object system using closures.

Read More  How do I dynamically create a Video object in AS2 and add it to a MovieClip?

The result is a more complicated programming language and a more complicated runtime model. Too complicated?

That’s the answer Does Java need closures?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you