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 ] ;
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 ;
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.
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 ] ;
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 ;
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.
EmoticonEmoticon