Is there no need to initialize variables if there are default values in Java? 【recording】

I was asked today what is the default value of boolean in Java? It is false in my mind, but I don’t know if you have noticed. In fact, although the basic types have default values, if they are not initialized, they are used directly or the output fails to compile, just go to the code:

Direct compilation error, prompting that it cannot be used without initialization: the reason is that the variable a is not initialized.

Insert picture description here
Because the JVM does not assign values ​​to local variables during class loading, it only assigns values ​​to static member variables. Therefore, when the class is loaded, local variables have no initial values, and we must consider them to be initialized.
Insert picture description here
Compilation is successful, output default value: false

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108539316