Java Knowledge Point 8

Source: Internet
Author: User
Tags naming convention

An array is a collection of variables of the same type, which can be referenced using a common name. An array can be defined as any type, which can be one-dimensional or multidimensional. One of the special elements in an array is to access it by subscript. Arrays provide a convenient way to group connected information.

Note: If you are familiar with C + +, be aware that the Java array works differently from them.

1. An array is not a collection, it can only hold references to multiple primitive types or objects of the same type. An array saves only the object's references, not the object itself.

2. The array itself is an object, and the object in Java is in the heap, so the array object itself is in the heap, regardless of whether it holds the original type or other object types.

3. Two forms of array declaration: one, int[] arr; II, int arr[]; It is recommended to use the former, which conforms to Sun's naming convention, and is easy to understand the key point, which is an int array object instead of an int primitive type.

4, it is always illegal to include the array length in an array declaration! such as: Int[5] arr;. Because no object is instantiated at the time of the Declaration, only the JVM allocates space when instantiating the array object, which is related to length.

5. The length must be specified when the array is constructed, because the JVM needs to know how much space to allocate on the heap. Counter example: int[] arr = new int[];

6. Declaration of multidimensional arrays. Int[][][] arr; is an array of three-dimensional int.

7, one-dimensional array construction. form: string[] sa = new string[5];

Or split into two sentences: string[] sa; SA = new String[5];

8, the default value of the primitive type array element. For primitive type arrays, the JVM automatically initializes them when the new construct is complete 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. such as: car[] MyCar = new CAR[10]; Equivalent from mycar[0] to mycar[9] are automatically initialized to mycar[i] = null;

10. Arrays of object types, although initialized by default, do not call their constructors. This means: car[] MyCar = new CAR[10]; only one MyCar array object is created! There is no instance of the car object created!

11, the construction of multidimensional arrays. Float[][] ratings = new float[9][]; The length of the first dimension must be given, and the rest may not be written, because the JVM only needs to know the length of the object assigned to the variable ratings.

12. The range of the array index. The index of each element in the array starts at 0, to Length-1. Each array object has a length property that holds the size of the array object. (Note that it is regrettable to distinguish between the length () method of the String object, which is not unified.) )

13, Java has an array subscript check, when access beyond the index range, will produce a arrayindexoutofboundsexception runtime exception. Note that this subscript check is not done at compile time, but at run time! i.e. int[] arr = new INT[10]; ARR[100] = 100; Such obvious errors can be compiled, but thrown at run time!

The Java array subscript check is an extra cost, but it's worth the security tradeoff, because many languages are unsafe when using arrays, and can arbitrarily access an array outside of their own memory block, and compile and run without error, producing unpredictable consequences!

Java Knowledge Point 8

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.