W3 Schools wrote:Expressions are limited. They have to immediately return a value, and they cannot contain variables, assignments or statements such as if or for. In order to do more complex operations, a code block can be used with curly braces. If the lambda expression needs to return a value, then the code block should have a return statement.
The part about assignments and variables is just wrong.
These are the only limitations, compared to a normal expression:
The Java Language Specification, Java SE 22 Edition wrote:Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be final or effectively final (§4.12.4), as specified in §6.5.6.1.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
https://docs.oracle.com/javase/specs/jls/se22/html/jls-15.html#jls-LambdaBody
It's fine to use variables in a lambda expression.
You can also use assignments, as long as the assigned variable is a lambda parameter. It doesn't actually do anything useful, but it's possible.