官术网_书友最值得收藏!

Understanding closures

As per the Mozilla Developer Network (MDN) -- "A closure is a special kind of object that combines two things: a function, and the environment in which that function was created."

In other words, the closure remembers the variables in the environment in which it was created. In Java, closures can be implemented using Lambdas. Let us discuss this with an example. Suppose we have a class ClosureExample as follows:

public class ClosureExample { 
   public static Function<Integer, Integer> closure() { 
      int a=3; 
      Function<Integer, Integer> function = t->{ 
          return t*a; // a is available to be accessed in this function  
      }; 
      return function; 
   } 
} 

This class consists of a function closure(), which returns a type of java.util.function.Function. In the previous example, variable a is in the scope of the function closure, so it is available to be accessed inside this function.

Then we have a nested function named function of type java.util.function.Function. This function takes an integer and multiplies it with a and returns the result. Then at last we are returning the nested function.

So according to the rules of scope, the value of a should vanish once a closure() call is finished. However, it is not the case. Let's call this function another class:

public class ClosureDemo { 
   public static void main(String[] args) { 
      List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); 
      Function<Integer, Integer> closure = ClosureExample.closure(); 
      list.stream().map(closure).forEach(n -> System.out.print(n+" ")); 
   } 
} 

If you run it, it multiplies every value by 3 and produces the following output:

3 6 9 12 15 

So the nested function in the function closure() is an example where it has taken a snapshot of the environment it was created in and then it is able to access the variable a which was in scope when it was created.

However, one interesting thing to note with closures is the Java. Any variable, which it uses and is defined outside its definition, it assumes that variable to be final or effectively final. So, in other words, in the example given previously, if you try to change the value of a inside the nested function, function then the compiler will not allow it:

int a=3; 
       
Function<Integer, Integer> function = t->{ 
             // a++  // will not compile    
      return t*a; // a is available to be accessed in this function  
  }; 

If you try to change the value compiler will throw an error saying: Local variable a defined in an enclosing scope must be final or effectively final.

主站蜘蛛池模板: 巴塘县| 勐海县| 教育| 二连浩特市| 河东区| 遵化市| 望谟县| 三门峡市| 靖江市| 扎赉特旗| 拉萨市| 南充市| 武隆县| 安庆市| 泊头市| 新乐市| 木里| 潼南县| 吉水县| 陈巴尔虎旗| 修文县| 鄂尔多斯市| 高淳县| 民勤县| 江门市| 西吉县| 广饶县| 遂宁市| 布尔津县| 迁西县| 克拉玛依市| 哈巴河县| 西宁市| 南华县| 安康市| 穆棱市| 阿荣旗| 扎兰屯市| 博客| 舟曲县| 罗平县|