Default initialization Based on java variables, scopes, and member variables (detailed description), java Variables

Source: Internet
Author: User

Default initialization Based on java variables, scopes, and member variables (detailed description), java Variables

In ava, variables include member variables and local variables. variables defined outside the class method become member variables or member fields (fields) to indicate the attributes of a class, the function of the variable defined as a member variable of the class is the whole class. This variable does not need to be initialized during definition. java will automatically initialize the member variable before use, automatic initialization of basic data types is as follows:

Java basic type default initialization value

Int 0
Short 0
Byte 0
Long 0
Float 0.0
Double 0.0
Boolean False
Char 0

For example:

public class test{ private int i; private short m; private byte n; private long l; private char c; private float f; private double d; private boolean b;  public static void main(String args[]){  System.out.println(i);  System.out.println(m);  System.out.println(n);  System.out.println(l);  System.out.println(c);  System.out.println(f);  System.out.println(d);  System.out.println(b); } }

The output of the above Code will be the default value after initialization;

For variables of the reference type, the default Initialization is null. Although java will automatically initialize the member variables, but automatic initialization will bring some errors, it is best to initialize them before using the variables, to ensure that the use of variables meets your needs, the default initialization function is only valid for Java member variables. If you want to use local variables, you must initialize them, otherwise, a compilation error is returned.

Java and c Use curly brackets to differentiate the start and end positions. The variables in the code block are valid only before the end of the code block. After the code block is exceeded, the variables are invisible and unavailable, for an object, its scope is always visible, knowing that the object has been recycled by the garbage collector, for example:

String s1 = new String("Hello world!");

The visibility of the reference variable s1 disappears at the end of the scope of the device, but the created String object will remain in the memory until the java Garbage Collector recycles its memory, although the String object will always exist in the memory, this object is unavailable because no reference points to this object.

The above default initialization Based on java variables, scopes, and member variables (detailed description) is all the content that I have shared with you. I hope to give you a reference and support for more.

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.