Comparison of arrays in Java, C, C + +

Source: Internet
Author: User

Arrays are a very common type of data, and in different languages it has similarities and places that are unique to each language, and here are some of the individuals ' understanding of arrays:

Same point:

1, whether in C + + or Java, arrays are the same type of data collection, although in the object-oriented language, because of the inheritance between classes, so that an array may contain seemingly different data types, But note that essentially they are inherited from the same type that is the type of the array (for example: for a fruit array: The values in the array may have apples, bananas, grapes ...). But they are all inherited from the fruit, that is, essentially the same data type);

2, once the array is initialized, the space in memory will be fixed, the array length can not be changed, even if the array is emptied, its length is still unchanged;

3, can be static initialization and dynamic initialization:

Static initialization: Explicitly specify the value of the array element, the system determines the length of the array; e.g.int[] A = {A/b} or int [] a = new int[]{1,2,3};

Dynamic initialization: Explicitly specify the number of elements of an array, with the system providing the initial value;

4, the array name represents the address of the array, but the address of the element is different for different languages = =

Different:

In C and C + +, because of the use of pointers, for arrays we often use arrays and pointers together, the array name represents the starting address of the array is the address of the first array element, and then every time from the address of the array name *n the length of the data type to get the address of the nth element, This is easy to use and often helps us to calculate the length of the array data types.

If a reference is placed in the array, then the reference address, which is contiguous, is stored in each element starting from the array name;

Arrays in Java:

In Java, an array is not just a collection of data, it is essentially a reference type, and the variable that he defines is a reference variable, the reference variable is stored in the memory stack, the value is the address in the heap memory of the object it refers to, and after the definition is represented as only a reference variable, without valid memory, So the definition cannot specify the length of the array (while C can, in the definition, specify the length, but if not initialize its value is not OK), when used, if the array is not initialized to assign a value will cause a null pointer exception;

There is also a reference to the object type can be either a basic data type or a reference type, but the value of the array element is also a reference to the object, to use its value is also required for each element of the new object, otherwise initialized their values are null, when used or will cause a null pointer exception, That is, an array of reference types needs to be initialized two times, and the array name in the memory stack points to the elements of the reference type in the memory heap, and each element points to the true object;

Two-dimensional arrays in Java:

From the above analysis, an array is a reference type, so a two-dimensional array should essentially be a reference to an array element to a one-dimensional array of one-dimensional arrays = =

type[][] = new type[len][], which is a one-dimensional array of type type[], we initialize len one-dimensional array of type type[], each type[] also requires subsequent initialization

Loop of the array:

In Java, you can use a foreach loop array without knowing the length of the array, but foreach cannot change the values of the array elements;

The array name represents the address of the array in the stack memory, the address of the array element is in the heap, and does not get the address of the element in the same way as C through the array name = =


There is not a whole place to hope that everyone will teach = =

Comparison of arrays in Java, C, C + +

Related Article

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.