Java Variable Classification and initialization, java variable Initialization
2017/6/25
The most authoritative java document is the official document. I read the document from the beginning today and clarify some small details.
Variable
The variables in Java are classified into the following four types:
1. Instance Variables: (Non-Static Fields) is a Non-Static field in the class.
2. Class Variables: Static field in the (Static Fields) Class
3. Local Variables: Local variable
4. Parameters: Parameters
Note the following two terms: field and variable. Field refers to the above 1 and 2, which are owned by class. Instead, the field variable is variable, corresponding to 3 and 4 above, or the local variable is variable.
Name
The recommended naming method is clearly stated in the Java documentation, which is why the naming methods in java code are almost the same. The format is to use continuous meaningful words, starting from the second word, with uppercase letters. For example: speed, currentGear
Primitive Data Type
Java has eight basic data types
Four integer types: byte, short, int, long
Two floating point: float and double
1 character: char
Boolean: boolean
Their default values are as follows:
| Data Type |
Default Value (for fields) |
| Byte |
0 |
| Short |
0 |
| Int |
0 |
| Long |
0L |
| Float |
0.0f |
| Double |
0.0d |
| Char |
'\ U000000' |
| String (or any object) |
Null |
| Boolean |
False
|
Default Value: Default Value.
C/c ++ does not help you set the default values of uninitialized variables. However, unlike java, the official documents clearly describe:
The eight primitive data types are: byte, short, int, long, float, double, boolean, and char. The java.lang.String class represents character strings. The compiler will assign a reasonable default value for fields of the above types; for local variables, a default value is never assigned. A literal is the source code representation of a fixed value. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
For Field, it is automatically set to the default value, for example, int to 0, and object to null. For Variable, It is not set automatically. If the compiler is not initialized, an error is reported.
Refer:
Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variablesummary.html
Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html