Page 1 of 1

About Question enthuware.ocpjp.v8.2.1819 :

Posted: Tue Jul 31, 2018 8:05 pm
by __JJ__
Remember that Double objects are immutable. Therefore, the first call to forEach does not change the elements in the original list on which the stream is based. It only replaces the elements of that stream with new ones.
Hi Admin
Sorry, but isn't this an issue about java being pass by value? Isn't the argument that the lambda uses a copy of a reference, analogous to how references work when passed to, and inside of, a method such that if you do

Code: Select all

Integer i = 10;
foo(i);
and then inside foo:

Code: Select all

void foo(Integer i){
i=10;
}
isn't that exactly what's going on with the lambda, ie it's not got anything to do with immutability, and it is everything to do with the lambda getting a copy of a reference, just as foo does, and so when that reference is reassigned, it makes no difference to the original object, because the reassignment takes place on a copy-of-a-reference? This is why the StringBuilder is modified: because it's a copy of a reference AND the operation takes place on the object pointed to by the copy-of-the-reference.

Thank you in advance.

Re: About Question enthuware.ocpjp.v8.2.1819 :

Posted: Tue Jul 31, 2018 11:05 pm
by admin
Yes, that is correct. I have updated the explanation to make it clear.

thank you for your feedback!
Paul.

Re: About Question enthuware.ocpjp.v8.2.1819 :

Posted: Wed Aug 01, 2018 9:08 am
by __JJ__
OK thank you very much.