Default initialization based on Java variables, scopes and member variables (detailed explanation)

Variables in AVA include member variables and local variables. Variables defined outside the methods in the class become member variables or member fields (domains), which represent the attributes of a class and are defined as the functions of the variables of the class's member variables. Therefore, the whole class does not need to be initialized when the variables are defined. Java will automatically initialize the member variables before use, The automatic initialization of basic data types is as follows:

Default initialization value of java basic type

For example:

The output of the above code will be the default initialized value;

Variables of reference type are initialized to null by default. Although Java will automatically initialize member variables, automatic initialization will bring some errors. Therefore, it is best to initialize variables before using them to ensure that the use of variables meets their desired effect; The function of default initialization is only valid for Java member variables. If you want to use local variables, you must initialize them, otherwise you will get compilation errors.

Java and c languages use curly braces to distinguish the start and end positions. Variables in a code block are only valid before the end of the code block. Beyond the code block, the variable is invisible or unavailable. For objects, its scope is always visible. It is known that the pair has been recycled by the garbage collector, for example:

The visibility of the reference variable S1 disappears at the end of the scope, but the created string object will always exist in memory until the Java garbage collector reclaims its memory. Although the string object will always exist in memory, because there is no reference to the object, the object is unavailable.

The above default initialization (detailed explanation) based on Java variables, scope and member variables is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>