How to initialize Java

Source: Internet
Author: User
Scope of the variables in Java:
1. Class Attribute, or value range
2. Local variables in the Method
3. method parameters

For the first variable, the Java Virtual Machine will automatically initialize it. If an initial value is given, the initialization is the initial value. If not, initialize it as the default initial value of the type variable.

The default value of int type variables is 0.
The default initial value of a float variable is 0.0f.
The default value of the double type variable is 0.0.
The default value of a Boolean variable is false.
The default initial value of the char type variable is 0 (ASCII code)
The default value of long type variables is 0.
The default value of all object reference type variables is null, that is, they do not point to any object. Note that the array itself is also an object, so the value of the uninitialized array reference after automatic Initialization is also null.

For two different class attributes, static attributes and instance attributes, the initialization time is different. The instance attribute is initialized when an instance is created. The static attribute is initialized when the class is loaded, that is, the class is used for the first time. The subsequent instance creation is not initialized again. This issue will be discussed in detail in future series.

The second type of variables must be explicitly initialized. If you try to use it before initialization, the compiler will protest. If the initialization statement is in the try block or if block, it must be assigned a value before it is used for the first time. That is to say, the compiler will protest when placing the initialization statement in the condition judgment statement that only contains the if block, because the execution may not meet the judgment condition after the if block, in this way, the initialization statement will not be executed, which violates the requirements that must be initialized before local variables are used. However, if there is an initialization statement in the else block, you can compile it, because in any case, at least one initialization statement will be executed and will not be initialized before use. The same applies to try-catch. If there is an initialization statement only in the try block, the compilation department will pass. If it is in catch or finally, it can be compiled. In short, make sure that the local variables are initialized before use. Therefore, a good practice is to initialize them when declaring them. If you don't know what the values will be, use the default values above!

In fact, the third and second variables are essentially the same. They are all local variables in the method. But as a parameter, it must have been initialized. The input value is the initial value, so Initialization is not required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.