Arrays in Java are different from those in C/C ++.

Source: Internet
Author: User

If you are familiar with C/C ++, Java arrays work in different ways. Arrays in Java are objects, which means they are fundamentally different from arrays in C ++.

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. Two forms of array declaration: 1. Int [] arr; 2. Int arr []; the former is recommended, which is an int array object instead of an int primitive type.

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.

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. Example: int [] arr = new int [5];

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)

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 it is distinguished from the length () method of the string object)

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 in Java can store both basic value types and objects. The object array and the original data type array are almost identical in usage. The only difference is that the object array contains references, while the original data type array contains specific values. When discussing the array, you must first determine whether the basic value type or object is stored in the array. Especially when debugging programs, pay attention to this aspect.

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.