Why are local variables not initialized in Java?

后端 未结 15 722
不知归路
不知归路 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条回答
  •  难免孤独
    2020-11-22 10:03

    if i am not wrong,Another reason could be

    Giving default value of member variables is part of class loading

    Class loading is a run time thing in java means when you create an object then the class is loaded with class loading only member variables are initialized with default value JVM does not take time to give default value to your local variables because some methods will never be called because method call can be conditional so why take time to give them default value and reduce performance if those defaults are never going to be used.

提交回复
热议问题