Variable might not have been initialized error

前端 未结 11 1621
孤城傲影
孤城傲影 2020-11-21 05:50

When i try to compile this:

public static Rand searchCount (int[] x) 
{
    int a ; 
    int b ; 

    ...   

    for (int l= 0; l

        
11条回答
  •  面向向阳花
    2020-11-21 06:02

    You declared them, but you didn't initialize them. Initializing them is setting them equal to a value:

    int a;        // This is a declaration
    a = 0;        // This is an initialization
    int b = 1;    // This is a declaration and initialization
    

    You get the error because you haven't initialized the variables, but you increment them (e.g., a++) in the for loop.

    Java primitives have default values but as one user commented below

    Their default value is zero when declared as class members. Local variables don't have default values

提交回复
热议问题