Why are local variables not initialized in Java?

后端 未结 15 1084
不知归路
不知归路 2020-11-22 09:28

Was there any reason why the designers of Java felt that local variables should not be given a default value? Seriously, if instance variables can be given a default value,

15条回答
  •  猫巷女王i
    2020-11-22 10:01

    It is more efficient not to initialize variables, and in the case of local variables it is safe to do so, because initialization can be tracked by the compiler.

    In cases where you need a variable to be initialized you can always do it yourself, so it is not a problem.

提交回复
热议问题