Arrays of objects are arrays of reference In Java you create
Arrays of objects are arrays of reference. In Java, you create arrays within arrays. Using the Internet, research how Java processes differ from other object-oriented languages such as C and C++? Explain your answer.
Solution
In java,you can create array of object as follows:
B[] objarray=new B[4];
it will create 4 array of object,but when you try to use this directly,it will throw null poinrer exception because you didn\'t initialise the array.
To avoid error,after above step we can do
objarray[0]=new B()
objarray[1]=new B()
objarray[2]=new B()
objarray[3]=new B()
That is we can initialise it,
Now after doing this we can use objarray[0].SomeFunction();
The coder must create both the array and element of array. Because each element of the array is a reference to a B, not the B itself.Hence ,it is neccessary to initialise it.
But ,if we do same thing in c++
B* b=new B[4];
In this,b will pointer to objects of array.We can directly use it b->someFunction().Because,they are initialise by constructor using new operator.Hence,no need to separate initialise
