Showing posts with label Core Java - Array. Show all posts
Showing posts with label Core Java - Array. Show all posts
In java Multi-dimension array defined as the array of array . Multi-dimension include two and three dimensions array.

2 dimension array --- > Collection of  1 Dimension array .
3 dimension array --- > Collection of 2-D array --- > Collection of 1- D array.

2-D Array

It is the collection of 1-D array . By defining two indexes in an array is called as 2-S array and its mandatory to indicate the size of the first index because its the starting point of any types of array, unless you give the 1st index size how it is going to allocate the memory for the second index as because 2-D is the collection of 1-D array.

Declaration of reference variable for 2-D array :

< data_type >   < ref var_name > [ ] [ ] ;
< data_type > [ ] [ ]   < ref var_name > ;
< data_type >   [ ] [ ] < ref var_name > ;
< data_type > [ ]   < ref var_name > [ ] ;
< data_type >   [ ] < ref var_name > [ ] ;

Creating the array Object :

< ref var_name >  =  new  < data_type > [ size 1 ] [ size 2 ] ;

Note 1: Don't indicate any while creating any reference because reference will only create only reference memory , it is not going to create any array object unless we use new operator to create array.

Note 2: While creating any 2-D array object it is mandatory to mention 1st index and from 2nd index onward size can be ignored. 

Why is it mandatory to maintain the 1st index size ?
It is because in case of 2-D array 1st index will hold the reference of the the next dimension array, that means here in 2-D each first index will hold the address of its attached 1-D array which contain the actual value.

Diagrammatic representation of 2-D Array

int   values [ ] [ ] =  new  int [ 3 ] [ 2 ] ; 


This is the representation of 2-D array where reference variable " value " pointing to the array object and 1st index holds the reference value for its corresponding 1-D arrays and that array contain the original value .  
Here we can see that each array contain its final length property holding the size of the array. 


Some of the Invalid statement for 2-D Array :

int   values [ ] [ ] = null ; 
values = new  int [  ] [  ] ;  // invalid array object because without  any index .
values = new  int [  ] [ 2 ] ; // invalid array object because without 1st index .

int   values [ 3 ] [ 2 ] = null ; // invalid array object as reference is with indexes. 

Some of the Valid statement for 2-D Array :

int   values [ ] [ ] = null ; 
values = new  int [ 3 ] [  ] ; // 2nd index can be left blank and that means 1st index in
                                                not containing any reference value as 2nd index is blank
                                                means no array . 


Diagrammatic representation of some of the valid statement of the 2-D Array

int   values [ ] [ ] = null ; 

 Here we can see that s imply a reference variable is created containing null , and hence not pointing to any 2-D array object. 
values = new  int [ 3 ] [  ] ;

Here we see that as per the statement a 1-D array is created containing null value and not pointing to its corresponding arrays because of 2nd index is left blank.

values [ 0 ] = new  int [ 2 ] ; 


Here we tried to store a 1-D store in the 1st index of the created array at the previous step. So now it is referencing to a 1-D array of size 2 containing zero as a default value because of int type array.





int  tmp [ ] = { 10 , 20 } ; 
values [ 1 ] = tmp ; 


The above statement indicates that we can even create our own 1-D and can store its reference to any valid 1st index of 2-D array because it contains only the reference of its referencing 1-D array but type of array must be same .
Note that here we are storing the reference the 1-D reference to
value [ 1 ] which was containing null previously . 

Like this we can also store any 1-D array of any length to value [ 2 ] which is still containing null value. for ex-

values [ 2 ] = new  int [ ] { 10, 20, 30 } ; 


If we are creating an array object then it will allocate memory and default value i.e zero is been set by the JVM in the allocated memory.

If we want to store the value at the time of declaring the array reference then we have the following statement,

< data_type >  < var_name > [  ]  =  {< value1 > , < value2 >,< value3 >.....} ;

int  value [ ] =  {  10, 20, 30 } ; // Valid statement 

int  value [ ] =  {  10, 20 ,30,  , } ; // Invalid statement 

int  value [ ] ;
value =  {  10, 20 ,30 } ; // Invalid statement it should be in one line .


 Anonymous Array 

Now if we have a requirement to assign the data at the time of creating the array object then we have the following statement and this is also known as Anonymous Array .

int  value [ ] =  new int[ ] {  10, 20, 30 } ; 

We can also assign one reference to another reference variable. Like this,

int  tmp [ ] ;
tmp = new int [ ] { 10,20,30 } ;

Note : We should not specify any size while creating the anonymous array . 

Important points must be taken care while using array in java

1> We cannot assign one reference variable to another at the same level or who does not have any inheritance relation . For ex-

int  arr [  ]  = new  byte [ 4 ] ;  // Invalid cannot store byte into int type . 

2> We cannot use + operator with the array reference for indicating the next member of the array because java do not deals with the address at the user level.

int  arr [  ]  = new  int [ 4 ] ;
( arr + 1 ) ;                          // Invalid in Java

3> Cannot assign direct a string value to a array reference . For ex-

String  arr  [ ] = " Java " ; 

The above statement is invalid and give compilation error saying " incompatible type " because compiler find the Java as a string type which is one of the class in java where as on the left side we have a String array. So it is " incompatible type " . Correct form is as following ,

String  arr  [ ] = { " Java " }  ; 

Now Java is a string is of array type. 
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 

Like all other programming languages , in java also it is defined as the collection of homogeneous data ( similar type of data ) but with different implementation that means in java Array is a reference type, which stores the reference value of array object. Now think, 

Why array stores only homogeneous type data ?

Its concern with the memory storage that means JVM will allocate the memory according to the data type defined with the array. If we define int type array then we cannot pass any floating value because for int JVM allocate 4 byte memory and where as float need 8 byte memory.

Syntax of Array definition :

[ modifier ]   < data_ type >   < ref var_name > [  ] ; 
[ modifier ]   < data_ type > [  ]   < ref var_name > ; 
[ modifier ]   < data_ type >    [  ]< ref var_name > ; 

Note: We cannot specify the size of array at the time of declaring reference variable.

Why we cannot define size of the array at the time of declaring reference variable ?
Here defining reference variable means it will create a memory of size 8 byte and stores the NULL value if we are not creating any array object with the use of new operator. Now think if we assign any size then is there any need of allocating memory for that sizes. Its all wastage of memory because reference variable is not indicating any array object unless any array object is created. That is why it will give compilation error. For ex-

int  a[ ] ;    // ' a '  is a reference variable of int type storing NULL. 
int[ ]  a ;    // valid statement 
int  [ ]a ;   //  valid statement 
int  a[ 10 ] ; // Compilation error because of size mentioned as 10. 

Creating the Array Object 

Previously we have seen how to declare array type reference, in this topic we will see how to create array object and how JVM is going to allocate memory .

Syntax for creating array object :

< data_ type >   < ref var_name > [  ] = new  < array_type > [ size ] ;
< data_ type > [  ]   < ref var_name > = new  < array_type > [ size ] ;
< data_ type >   < ref var_name > [  ] = new  < array_type > [ size ] ;

                                  or 

< data_ type >   < ref var_name > [  ] ;
< ref var_name >  =  new  < array_type > [ size ] ;

for ex-
int  a[ ]  =  new  int [ 2 ] ;          or                  int  a[ ] ;
                                                                        a =  new  int [ 2 ] ;

Above statement will create an int array object of size two containing default value zero and a reference variable will now contain a reference variable indicating array object.

Note : Reference value can never be a address of array object , it is just an system generated number used to indicate the array object. 

Some of the important points regarding declaration of array object 

1 >  While creating its mandatory to mention the size of array object otherwise it will give compilation error saying " array dimension missing " and size must be int compatible type i.e char, byte, byte and int . 

2> Let suppose we are creating array of specified size and if that much memory is not available then compiler JVM will throw run-time error as " Java.lang.OutOfMemory " .

3> We cannot define any negative size array otherwise it will give a run-time exception saying " Java.lang.NegativeArraySizeException " . for ex -
            
int  a[ ]  =  new  int [ -2 ] ;  // Invalid, array size can never be negative. 

4> While allocating for the array object JVM will maintain a length variable containing the length of the array. And length will return the int value which stores the size of the array. It is a final property that its content cannot be changed or modified at the run-time.

5> If we are trying to access any index which is not present in the array then it will give runt-me exception saying " Java.lang.ArrayIndexOutOfBoundException " . For ex-

int  a[ ]  =  new  int [ 2 ] ; 
System.out.println ( a [ 3 ] ) ; 

In the above code array maximum size is 2 and we are trying to access its 3rd index which is not available so JVM will give run-time exception ." Java.lang.ArrayIndexOutOfBoundException ".

6> If we are trying to access any array member of the array through a reference variable which contain null variable then JVM will throw run-time exception saying " java.lang.NullPointerException " .

int  a[ ] = null ;

System.out.println ( a[ 0 ] ) ;

In the above statement a is reference variable and it stores the null value. Accessing any member of the array give " NullPointerException " because reference variable not pointing any array object .

Let me make you more clear how an array object is creating and how a reference variable is referencing an array object. for ex-

int  a[ ]  =  new  int [ 3 ] ; 


Array object
Here you can see that a reference variable a created whose memory is 8 byte containing reference value i.e 92345(random generated number) and pointing to an array object of size 3 and a length property of 4 byte a created with hold the size of the array. 





int  a[ ] = null ;   
  
Array reference variable


 A reference is created of default size 8 byte containing null value. 






Note : In java once memory is created then size cannot be modified where as the object reference in the reference variable can be modified. 

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic