How are Java arrays different that conventional array of C and C++?
Ans: In C and C++ space for arrays is allocated statically during compilation. As against this space for arrays in Java is allocated dynamically at the time of execution of program. As a result, during execution the size of the array can be increased or decreased as required.
in a[ ] ;
a = new int[5] ; // creates an array of 5 integers.
// code to increase array size
int b[ ] ;
b = new int[10] ;
for ( int i = 0 ; i < 5 ; i++ )
b[i] = a[i] ;
a = null ;
In C and C++ space for arrays is allocated statically during compilation. As against this space for arrays in Java is allocated dynamically at the time of execution of program. As a result, during execution the size of the array can be increased or decreased as required.
in a[ ] ;
a = new int[5] ; // creates an array of 5 integers.
// code to increase array size
int b[ ] ;
b = new int[10] ;
for ( int i = 0 ; i < 5 ; i++ )
b[i] = a[i] ;
a = null ;