Why aren't unassigned local variables automatically initialized?

前端 未结 6 1569
长发绾君心
长发绾君心 2021-01-02 06:28

It seems like there is no way to have unassigned local variables in your code or check for them, as the compiler spits out Use of unassigned local variable erro

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 06:58

    1 Why does the compiler not allow the use of uninitialized variables?

    Because preventing that promotes good programming.

    2 Why does the compiler allow the use of uninitialized class members?

    Because it's not possible to track this with any accuracy.

提交回复
热议问题