Thursday, March 30, 2017

Assign one array reference into another

Assign one array reference into another


Let us discuss about how to assign a array reference into another array reference and how the memory is being created and how value are being stored in the array. Let me make you more clear by taking an example ,

int  arr [ ] = new  int [ 3 ] ;

Showing Array Object creation


Here arr is int type array reference which is pointing to the array object of size 3.





int  value [ ] = arr ;


Other Object referencing Array Object
Here we can see that by assigning a array reference into another then the reference value of one is copied to another.Now both are pointing to the same array object as both are having same reference value i.e 92345 .If we change the value through any reference will reflect in both the references.



How to check whether both contain same value or if we change value through one reference value will also reflect that change on another?

int  arr [ ] = new  int [ 3 ] ; 
arr [ 0 ] = 40 ;
System.out.println( " Before changing :"+arr[ 0 ] );
int  value [ ] = arr ;
value[ 0 ]= 60 ; 
System.out.println( " Before changing :"+arr[ 0 ] );

By executing the above code we will get the output value changed from 40 to 60 that means both the reference value pointing to the same array object and if we change any member of array then it will reflect the change on another reference variable.

What if we declare the array variable as final

If we declare the array variable as final then we cannot change the value of the reference variable that means it cannot point to another array object but we can change the array member variable because by declaring final it will only fix the reference value and hence cannot be modified. for ex-

final int  arr [ ] = new  int [ 3 ] ; 

Final Array object
Here by declaring the reference value i.e 92345 here cannot be modified in future that means we cannot assign another array object through this reference variable but if we try to copy this to another reference variable it will work fine because we are not modifying the value, simply copying value from one to another.


arr [ ] = new int [ 4 ] ;  // Compilation error 

The above statement will give compilation error saying " cannot assign a value to final variable arr here.
But we can modify the member of array because members in not declared as final, it can be modified, like,

arr [ 0 ] = 60 ; // It replaces the default value to 60. 

Note : Since array is an object so the array reference can be stored in the object reference due to inheritance property and we know that object class is the super class of all the classes. So we can store the sub- class references into super class object. 

int  arr [ ] = new  int [ 3 ] ; 
Object ref = arr ;              // It is valid because of inheritance property .
int  value [ ]  =  ref ;        // Invalid because of inheritance property that we cannot
                                           assign again super-class reference to sub-class reference. 


Even we cannot access the array members through object class reference , like this
ref [ 0 ] ;   // Invalid 


Available link for download