Java: Why is the compiler complaining here?

  0          int specialFuncStart;
  1          String[] specialFunctions= {"sum","ddx","integral"};
  2          for(String element: specialFunctions){
  3              specialFuncStart = finalMath.lastIndexOf("sum");
  4          }
  5          while (specialFuncStart != -1) {   code  }

Why line 5does it say that it specialFuncStartmay not be initialized? Lines are final and fixed, so the for loop will always work. Doesn't the compiler know this, or am I missing something? I understand that initialization specialFuncStart = -1;is how to fix it, but why?

+4
source share
3 answers

You declared it, but you did not initialize it. You need to set the value for it:

int specialFuncStart; // declare
specialFuncStart = 0; // initialize

int specialFuncStart = 0; // both

Your compiler believes that sometimes specialFuncStartit will not be initialized, since the loop forshould not be executed.

+2

, specialFuncStart for, , , 0, .

, , ,

specialFuncStart = 0;

0

while , . , , , , , , , . , Java - , .

0

Source: https://habr.com/ru/post/1624650/


All Articles