Java syntax Summary-array
An array is a set of variables of the same type and can be referenced by a common name. Arrays can be defined as any type, which can be one-dimensional or multi-dimensional. A special element in the array is to access it by subscript. Arrays provide a convenient way to group associated information. Note: If you are familiar with C/C ++, note that Java arrays work in different ways.
1. An array is not a set. It can only store multiple original types or object references of the same type. Arrays only store object references, not objects.
2. the array itself is an object, and the objects in Java are in the heap. Therefore, no matter whether the array stores the original type or other object types, the array object itself is in the heap.
3. Two forms of array declaration: 1. int [] arr; 2. int arr []; the former is recommended, which complies with Sun's naming rules and is easy to understand, this is an int array object, not an int primitive type.
4. It is always invalid to include the array length in the array declaration! For example, int [5] arr ;. This is because no object is instantiated during declaration. The JVM allocates space only when the array object is instantiated, which is related to the length.
5. length must be specified during Array Construction, because the JVM needs to know how much space needs to be allocated on the stack. Inverse example: int [] arr = new int [];
6. Multi-dimensional array declaration. Int [] [] [] arr; is a three-dimensional int array.
7. Construct a one-dimensional array. For example, String [] sa = new String [5]; or String [] sa; sa = new String [5];
8. Default Value of the original type array element. JVM automatically initializes an array of the original type when it is constructed with new without initialization. Default Value: byte, short, int, long -- 0 float -- 0.0f double -- 0.0 boolean -- false char -- '"u0000 '. (Whether the array is a member variable or a local variable)
9. The reference in the object type array is initialized to null by default. For example, Car [] myCar = new Car [10]; equivalent to myCar [0] To myCar [9] is automatically initialized to myCar [I] = null;
10. Although the object type array is initialized by default, its constructor is not called. That is to say: Car [] myCar = new Car [10]; only one myCar array object is created! No instance is created for the Car object!
11. Multi-dimensional array construction. Float [] [] ratings = new float [9] []; the length of the first dimension must be given, and the rest can be left blank, because JVM only needs to know the length of the object assigned to the variable ratings.
12. array index range. The index of each element in the array starts from 0 to length-1. Each array object has a length attribute, which saves the length of the array object. (Note that the length () method of the String object is distinguished. It is a pity that the two are not unified .)
13. Java has array subscript check. When the access is out of the index range, ArrayIndexOutOfBoundsException will run abnormally. Note that this subscript check is not performed at the Compilation Time, but at the runtime! That is to say, int [] arr = new int [10]; arr [100] = 100; such an obvious error can be compiled, but thrown at runtime! The array subscript check in Java requires additional overhead, but it is worthwhile to balance the security because many languages are insecure when using arrays, you can access any array outside your memory block. No errors will be reported during compilation and operation, causing unpredictable consequences!