2. What are the steps to create an array?
Answer
- Declaration.
- Construction.
- Initialization.
Declaration of an Array.
boolean booleans[]; int ints[];
byte bytes[]; float floats[];
short[] shorts; long longs[];
char chars[]; double[] doubles;
Note: Declare an array of objects Button buttons[];
Note: In an array declaration, square brackets can be placed either before or after the array name. The declaration of an array does not allocate any storage, it just declares the array name.
Construction of an Array
Using new keyword an array can be constructed. When you are constructing an array using new keyword, the size of the array must be provided. In Java arrays knows their size.
short shorts[];
shorts = new short[5];
Declaration - Construction in a single line
int ints[] = new int[4];
Declaration - Construction - Initialization in a single line
int ints[] = new int[]{10,20,30,40};
Note: When an array is constructed, it's elements are automatically initialized to the default values, if there is no explicit initialization. |