This content has been marked as final. Show 3 replies
The error message you get tells you what the difference is.
if(true) is evaluated by the compiler and the code inside the if statement is always executed.
In the second version b==true could be false. The compiler doesn't look at the previous line where b is set to true.
First one is simply deterministic such that the compiler can be sure that 'hoursInDay' will have a value.
Second is not. The fact that you know what it is doing doesn't mean the the compiler does. Nor is it efficient for it to try all execution paths. So it doesn't think 'hoursInDay' will be set so it give you an error.
Thanks to all who replied to the question.