true
is a constant expression, the compiler can deduct its value compile time.
On the other hand, gg
is not constant, but a variable, that you could initialize with anything (even an expression, a return value of a function, etc). So, the compiler does not check how did you initialize that variable, is assumes that it can be anything.
That's why the compiler thinks that the variable g
can or cannot be initialized.
How deep do you think the compiler can go? Take this one, for example:
boolean gg = Math.random() >= 0.5;
gg = gg ? gg : true;
We know that gg
will be true, but how does the compiler know it?
Or another example:
boolean gg = (Math.random() >= 0.0);
Again, we know that it is always true (because random()
is between 0.0
and 1.0
), but the compiler doesn't know.
So, it's not an easy task for a compiler to do, therefore the compiler creators decided to check only the constant expressions when predicting execution flow, and not to guess the variable values.
That's the reason why you get a warning in one case and don't in the other.