Home > Back-end >  About lambda expressions to access external variables has a very important limit a little confused
About lambda expressions to access external variables has a very important limit a little confused

Time:10-05

Lambda expressions to access external variables has a very important limitations: variable immutable,
Could you tell me why:
String [] array={" a ", "b", "c"};
For (Integer I: Lists. NewArrayList (1, 2, 3)) {
The Stream of (array). The map (item - & gt; Strings. PadEnd (item, I, '@')). The forEach (System. Out: : println);
} can perform?
And:
String [] array={" a ", "b", "c"};
For (int I=1; i<4. I++) {
The Stream of (array). The map (item - & gt; Strings. PadEnd (item, I, '@')). The forEach (System. Out: : println);
} is not? The two I are local variables, and not the final?

Is in the process of lambda statement execution immutable is ok? I don't feel quite right, I'll find some foreach source code, is it because I in the first point is the next in the iterator () method returns the object that is constant, and according to
Public E next () {
CheckForComodification ();
Try {
Int I=cursor;
E next=get (I);
LastRet=I;
Cursor=I + 1;
Return next;
} the catch (IndexOutOfBoundsException e) {
CheckForComodification ();
Throw new NoSuchElementException ();
}
}
Next () method returns the object is changing, so temporary variable I is final,
Don't know understand right, wish you a great god can give directions, thank you

CodePudding user response:

Enhanced for loop, the above the first one can be rewritten as iteration statements, see clearly like this:
String [] array={" a ", "b", "c"};
List List=Arrays. AsList (1, 2, 3);
Iterator The iterator=list. The iterator ();
While (iterator. HasNext ()) {
Int I=iterator. Next ();
The Stream of (array). The map (item - & gt; Strings. PadEnd (item, I, '@'))
ForEach (System. Out: : println);
}
At the beginning of the while statement block, I only once, in line with the efficient final conditions,
And common lambda expressions in the for loop in reference external variable I, as you can see there are obvious change traces, so it is not allowed,

CodePudding user response:

Basically see lambda expressions refer to external variables, the scope of the scope of if the variable is not changed within their scope (variable does not appear twice), even if there is no limit to the final character, is also counted as effectively final,

CodePudding user response:

for(int i=0; i<100; I++) {
Int k=I;
K=k;
Pool. Execute (() - & gt; {
For (int j=0; J & lt; 1000; J++) {
System. The out. Println (k);
Mydata2. AddPlusPlus ();
}
System. The out. Println (Thread. CurrentThread (). The getName () + "-" + mydata2. Number);
});
}
A piece of code can be clearly seen from above, the variable k is cannot use now, however, get rid of the k=k, then the variable k {} in this section can be regarded as the scope of the range did not change, as effectively final, lambda expressions to be passed,

CodePudding user response:

Because of that, in the end, the lambda expression will be compiler into an inner class, and inner classes access to a local variable, must request the local variable immutable (namely the upstairs said effectively final , only an assignment operation), why? Because Java does not support closures, a class (including the inner class) can access another class member variables, but was not able to access another kind of the local variable inside the method, the Java internal classes is how to support access to a local variable? The answer is that the compiler will be referenced to a local variable, implicitly to the inner class, as its member variable, because this kind of practice, so must be asked, if local variables should be immutable,
You can refer to this answer
  • Related