The difference between arrays in java/c++

Source: Internet
Author: User

1. Array name differences

--------------------------------------

1. In Java, it goes without saying that, in the principle of all objects, arrays in Java are also objects. So what is the array class, of course not java.util.Arrays. See the Java array approach

2. Whereas in C + + the array name is actually a data structure, some people would say that it is not a pointer, see (in C + + the array name is actually a data structure).

From the above two points we can see that the Java array name is an object, and the C + + array name is a data structure. So what's the difference between an object and a data structure? See the differences between data structures and objects.

2. Principle Differences

-----------------------------------

The memory allocation in the Java array is in the heap , must be allocated with new, and C + + inside the stack is allocated , the definition will be automatically assigned.

Arrays in Java

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. Two forms of an array declaration: one, int[] arr;  II, int arr[]; It is recommended to use the former, which is an int array object instead of an int primitive type.

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.

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

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)

10. Arrays of object types, although initialized by default, do not call their constructors. (called in C + +) i.e.: 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 distinguished from the length () method of the String object)

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 array in Java can store both the base value type and the object. The object array and the original data type array are almost identical on the method used, except that the object array holds the reference and the original data type array holds the specific value. When discussing questions about arrays, be sure to first determine whether the array stores the base value type or the object. Especially when debugging a program, be aware of this aspect.

3. Whether the class array calls the constructor

-------------------------------------------------

The array of the 10th object type mentioned above, although initialized by default, does not call its constructor. I have to focus on explaining that because it calls its constructor in C + +, it's easiest to confuse.

[Java]View Plaincopyprint?
  1. Public class Test extends JPanel {
  2. private Static final long serialversionuid = 4767050156491994899L;
  3. public static void Main (string[] args) {
  4. animapp[] Array = new animapp[3];  //There is no execution constructor, only space is applied here.
  5. array[0] = new Animapp (3); //Construct an object
  6. }
  7. }
  8. Class Animapp {
  9. Public animapp (int times) {
  10. System.out.println ("Class Animapp");
  11. }
  12. }


Look at the Java code above, when executing animapp[] array = new ANIMAPP[3]; The constructor is not called and only space is requested. The constructor is called only when the object is constructed.

Java, when defining an array, new[], did not execute the constructor, just requested a memory space, with C + + allocator<t>.allocate (size) (because allocator<t The type of > is determined at run time, so it is not the number of bytes in the specified space, but rather the number of elements, the size of each element allocator in the record). Then, when constructing an object, as in the case of New Animapp (3), the object is actually constructed, like Allocator<t>.construct (param).


in C + + , the new[] operator (the default) would have to apply for space first, then execute the constructor, how many objects were requested for space, how many times (once per object), and so it is not possible to use the default new[] to define an array for a class without a parameterless constructor, This is different from Java, he is to apply for space, but also to construct objects, Java just apply for a space, for each object in the space, you have to display the new ClassName (param) to construct.

The reason for Java to be different from C + + is that the implementation differs between them.


1) An array of Java 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) in Java, the official recommended way of declaring is Object []array, and cannot specify the length, only the length is specified when new.
4) references in array of object types are initialized to null by default. such as: Object [] myobj= new Object [10]; Equivalent from myobj[0] to myobj[9] are automatically initialized to mycar[i] = null, and no constructor is called. Called when myobj[1] = new object.

As for why Java chooses this way, it deliberately avoids pointers (which are prone to mistakes) and symbols are reference types.

Arrays are supported in almost all programming languages. It is dangerous to use arrays in C and C + +. Because arrays in C and C + + are blocks of memory. If a program accesses an array other than its own block of memory, or uses it before the array is initialized, it can have unpredictable consequences.

One of the main goals of Java is security. Java ensures that the array is initialized and cannot be accessed outside of its scope. This range check is at the expense of a small amount of memory overhead on each array and the run-time subscript check. But the increase in security and efficiency is also worthwhile.

When Java creates an array object, it actually creates a reference array, and each reference is automatically initialized with a special value. The value has its own keyword NULL, and once Java sees NULL, it knows that the reference is not pointing to an object. If you use a reference that points to null, you will run the times wrong.

The difference between arrays in java/c++

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.