Is closure supported in Java 7?

Source: Internet
Author: User

Of course, it is not the closure for operations in algebra, but the closure in function programming. Here is the proposedPDF. From the PDF, Java is ready to support the passing lexical scoped closure. That is to say, the free variables read by a function object must be determined by the source code structure. Let's look at the example:

Public static void main (string [] ARGs ){
Int plus2 (int x) {return x + 2 ;}
INT (INT) plus2b = plus2;
System. Out. println (plus2b (2 ));
}

This sectionInt plus2 (int x) {return x + 2 ;}A function called plus2 is defined. It accepts an integer parameter and returns an integer. The next sentence int (INT) plus2b = plus2; declares a function variable (reference type), plus2b, and points it to the function referred to by plus2. INT (INT) is the type of a function variable. It indicates an integer parameter. An integer parameter is returned.

Corresponding closure statement:

INT (INT) plus2b = (int x) {return x + 2 ;};

Or:

INT (INT) plus2b = (int x): x + 2

In fact, Java should start to support type inference ). The function plus2 already has type information, and the above sentence can be completely

It can be written as plus2b = plus2. As in C #3.0, writing var plus2b = plus2 makes the compiler somewhat explicit.

Some old Conference said that it is just syntactic sugar. Aynonymous class supports lexical scoped closure. Well, no. Anonymous class is a free variable that can read within the lexical scope, but these variables must be final. However, the new Java closure can indeed read any local variables, including function parameters (otherwise, how can we use curry? ). Word: Cool!

Of course, things are never that simple. The proposal also discussed several other key issues. For example, how to be compatible with old code, how to distinguish it from Anonymous class, how to perform type conversion, and how to support generics. If you are interested, read PDF.

It is certainly good news that closure is available in Java 7. At least we don't have to worry about Java as the term's kingdom. Sun said that Java wantsSupports more languages(Including VB. NET. How does it sound like. Net?) MoreDynamic Language Features. Unfortunately, it takes several years to use Java 7. At least my company is still using 1.4. Of course, which version to use depends on many factors, and the language itself has little impact. However, this is another topic. Sun's actions should be happy. Although. Net has a development advantage, the world is so cruel: we only look at the results, don't listen to the reason. LookC # new features in 3.0Right: Extension Method, Lambda expression (this is the same as closure in Java), new initializier (do you not like S = [1, 2, 3, 4] Such concise syntax ?), The value belongs to the anonymous type... In terms of language improvement, I have to say that Microsoft is very quick.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.