Java: “Local variable may not have been initialized” not intelligent enough?

前端 未结 3 1946
醉酒成梦
醉酒成梦 2020-12-17 03:49

Consider the following method:

void a ()
{
    int x;
    boolean b = false;
    if (Math.random() < 0.5)
    {
        x = 0;
        b = true;
    }
            


        
3条回答
  •  攒了一身酷
    2020-12-17 04:15

    There is one :

    void a () {
        if (Math.random() < 0.5) {
            int x = 1;
        }
    }
    

    The compiler isn't responsible for devising and testing the algorithm. You are.

    But maybe you should propose a more practical use case. Your example doesn't really show what's your goal.

提交回复
热议问题