Java language:
When initializing a variable, you can declare it first, assign a value, or initialize the variable while declaring it. If you do not initialize a variable of the basic type, the system also initializes the variable. However, for an array of the basic type, the system will initialize the array; for an array of the object type, the system will not perform such operations. Example:
Public class test03 {static int X; static float y; static string Z; static int arr [] = new int [10]; static float ft01 [] = new float [10]; static float ft02 [] = new float [10]; static string str01 [] = new string [5]; static string str02 [] = {"AAA", "BBB "}; public static void main (string ARGs []) {system. out. println ("x =" + x); system. out. println ("Y =" + Y); system. out. println ("z =" + Z); system. out. println ("arr [1] =" + arr [1]); system. out. println ("ft01 [1] =" + ft01 [1]); system. out. println ("t02 [1] =" + ft02 [1]); system. out. println ("str01 [0] =" + str01 [0]); system. out. println ("str02 [0] =" + str02 [0]) ;}}
Result After running:
X = 0y = 0.0z = nullc = arr [1] = 0ft01 [1] = 0.0t02 [1] = nullstr01 [0] = nullstr02 [0] = aaa
As you can see, the system will automatically initialize variables or arrays of the basic type:
Int ----> 0
Float/double-> 0.0
Boolean --> false;
Char -->''
Encapsulation types corresponding to objects or basic types:
String/float/Doube/Integer/Boolean is not initialized, so it is null
C language Initialization
Char STR [10] = {0}, where 10 elements are assigned a null character (null character), which is '\ 0 '. Print the value of STR [0] directly and you will find that it is a NULL Character and nothing is displayed. However, after forced conversion, you will find that its ASCII value is 0, it occupies one byte, of which 8 digits are all 0.
# Include "stdio. H "# include <string. h> int main () {char str2 [] = {'\ 0',' \ 0', '\ 0'}; char str1 [10] = {0 }; int Len = strlen (str2); int len1 = strlen (str1); char a = '\ 0'; printf ("Len = % d; len1 = % d, A = % C \ n ", Len, len1, a); printf (" c = % C \ n ", str1 [0]); char STR [2, 200]; while (gets (STR) {printf ("str [0] = % C, STR [1] = % C \ n", STR [0], STR [1]); If (STR [0] = '0') printf ("str [0] value print output "); if (STR [0] = '0' & STR [1] = 0) break;} return 0 ;}
Char A = '\ 0'; // The value is a rest character. If it is printed blank, nothing is displayed, but it occupies the position of a character and is not a space, because the ASCII value is 0, not 32
Char STR [] = {'\ 0',' \ 0', '\ 0'}; // defines an array. All three elements are break characters, and the sizeof result is 3, but the strlen result is 0.
Char str1 [10] = {0}; // This assignment is actually to assign 10 null characters, that is, '\ 0', And the ASCII code is 0.
About initializing Arrays:
1 The specified initialization value cannot be more than the array element, but can be less
2 If there are few, the initial values in the list will be allocated to the continuous elements starting from the first element, and those elements that do not have the initial values will be initialized to 0 --- explicitly the ASCII value is 0
3. If the initialization list is not provided at all, this is not the case. The array will contain useless data, that is, the original value in the memory.