Arrays
Arrays are indexed collection of homogeneous data elements.The main advantage of arrays is we can represent huge number of values by using single variable so that readability of the code will be improved.
But the main disadvantage is ,it is fixed in size.ie, once we creates an array there is no chance of increasing or decreasing the size based on our requirement.And knowing the size of the array in advance may not be possible always.
ARRAY DECLARATION
int[ ] x;
OR
int [ ]x;
OR
int x[ ];
Size of the array is specified only at creation not during declaration.
ARRAY CREATION
Every array in java is an object.Hence we can create arrays using 'new' operator.
int[ ] a = new int[3];
'a' is the reference variable
ARRAY INITIALIZATION
int[ ] x = new int[3];
x[0]=1;
x[1]=2;
x[2]=3;
ARRAY DECLARATION,CREATION AND INITIALIZATION IN A SINGLE LINE
int[ ] x= {10,20,30};
char[ ] ch= {'a','e','i','o','u'};
String[ ] s= {"A","AA","AAA"};
length vs length( )
length is a final variable applicable for arrays. length variable represents the size of the array.
eg: int[ ] x = new int[6];
System.out.println(x.length);
Output: 6
length( ) is a final method applicable for string object. length( ) returns the number of characters present in the string.
eg: String s = "Hai";
System.out.println(x.length( ));
Output : 3
Comments
Post a Comment