Arrays

In Java, when you declare an array of a class, you could say:


	Integer[] a = new Integer[5];

But this creates only an array of references, you must then instantiate each reference, using

	a[1] = new Integer(); 

Even for the default constructor (as shown). In C++, when you declare:

	Integer[5];

C++ would call the default constructor for each instance of the array, and there would be no need to explicitly call 'new' again to instantiate each reference of the array.

This form of initialisation only allows you to call the default constructor (or constructor with no parameters). In C++, you can create an array of objects, with each object sitting right next to each other in memory, which lets you move from one object to another, simply by knowing the address of the first object. This is impossible in Java.

In C++ you can also create arrays, of pointer or references to objects. This is more closely related to how Java arrays work. Java arrays are always an array of references to objects (except if you create arrays of the basic data types, int, float, double etc.). If you were to use an array of pointers or references, you would have an array of null references (just like Java). The syntax would be like: Integer **f = new Integer*[10];

This C++ code segment shows how to create an array of A objects arr[10] as the state of the B class, and have the non-default constructor called for the objects.

#include <iostream>
#include <string>

using namespace std;
 
 class A{
   private:
     int x;
   public:
     A(int);
     A();
     virtual void display();
   };
   
   A::A() {}
   A::A(int y): x(y) {}
   
   void A::display()
   {
     cout << "Object has the value " << x << endl;
   }
   
   class B{
     private:
       A arr[10];  //default A constructor A() is called here (10 times).
     public:
       B();
       virtual void display();
   };
   
   B::B()
   {
     for(int i = 0; i < 10; i++)
       arr[i] = A(i);	//the non-default constructor 
		              // A(int) is called here (10 times).
   }
   
   void B::display()
   {
     for(int i = 0; i < 10; i++)
       arr[i].display();
   }
   
   int main(){
     B b;
     b.display();
   }

A Java array is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity is worth the expenseJava has run-time bounds checking on arrays, throwing an exception when an operation is performed that is out of bounds. There is a special instance variable called length that holds the number of elements in the array. Arrays in Java must be created using the new keyword.

When you create an array of objects in Java, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword null. When Java sees null, it recognises that the reference in question is not pointing to an object. You must assign an object to each reference before you use it, and if you try to use a reference that is still null, the problem will be reported at run-time. Thus, typical array errors are prevented in Java. You can also create an array of primitives. Again, the compiler guarantees initialisation because it zeroes the memory for that array.