Types and initial values of variables in Java

Source: Internet
Author: User

Variable Classification

1.
Class variable

L
Use the static keyword to modify fields in the class definition

L
Fields used in interface declaration or without static Modification

Note: When a class or interface is ready, a class variable is created and initialized as a default value. When a class or interface is uninstalled, the class variable will be effectively stopped.

 

2.
Instance variables

Fields declared without the keyword static in the class declaration

 

3.
Array elements (untitled variables)

Whenever you create a new object as an array, these variables are created and initialized to the default value. When the array is not referenced, it will effectively stop the array element.

 

4.
Method parameter variables

For each parameter declared in the method declaration, a new parameter variable is created when the method is called. The new variable is used to initialize the corresponding parameter values of the method call. When the method body is executed, the method parameter is stopped.

 

5.
Constructor Parameter Variable

For each parameter declared in the constructor, a new parameter variable is created every time a class instance creates an expression or shows that the constructor calls this constructor. The new variable is initialized with the corresponding parameter values from the creation expression or constructor call. When the constructor is executed, the constructor parameters are stopped. ,

 

6.
Exception Handling program parameter variables

When an exception is caught by a catch or try statement, an exception handler parameter is created. The new variable is initialized with the actual object associated with the exception. When the code block associated with the catch clause is executed, the exception handler parameters are stopped.

 

7.
Local variable

Declare the variables declared by the statement using local variables. Assign values before using local variables. Local variables are not automatically initialized to the default value.

 

8.
Final variable

Final-modified variables can only be assigned once. Unless you explicitly cancel the assignment of the fianl variable before the assignment, assigning a value to the final variable will cause a compilation error.

 

Initial Value of a Variable

Before using the variable value, each variable in the program must have a value:

1.
Each class variable, instance variable, or array element will be initialized with the default value during creation. The following is the correspondence between the data type in Java and its default value.

Byte, short, INT: 0

Long: 0l

Float: 0.0f

Double: 0.0d

CHAR: NULL character (I .e.:/u0000)

Boolean: false

Application Type: NULL

2.
Parameters of each method are initialized to the corresponding parameter values provided by the method caller.

3.
Each constructor parameter is initialized to create an expression for the class instance or display the corresponding parameter values provided by the constructor call.

4.
The exception handler parameter will be initialized to throw a duix that indicates an exception

5.
Before using a local variable, you must assign a value to it through initialization or assignment display. The compiler can use a clear assignment rule to evaluate this assignment method by ing.

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.