Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ - pink underlined variable... what does it mean?

could you please explain me, why the list-variable in line 108 has this specific color and is underlined?

enter image description here

If i put the same code in a for-each instead of a foreach(), then it looks "normal".

Thank you in advance!

like image 330
Chris Avatar asked Feb 03 '23 20:02

Chris


1 Answers

For the first thing you're using lambda which is syntactic sugar. At the second thing the purple underline appears if a variable isn't from inside the lambda to show the user you're not using a local variable.

For example:

public static void main(String[] args) {
    int a = 0;
    Consumer<String> stringConsumer = foo -> {
      foo += a;
    };

    for (int i = 0; i < 1; i++) {
        String s = " " + a;
    }
}

If you paste that you'll see that only the a in foo += a gets purple underlined because it's from outside the lambda.

like image 158
CodeMatrix Avatar answered Feb 06 '23 11:02

CodeMatrix