Page 280 - Computer science 868 Class 12
P. 280
They are as follows:
a. Declaration of the array: It is the same as the variable declaration.
Syntax:
datatype name_of_array[] or
datatype[] name_of_array
b. Memory allocation: Every variable declared takes space in memory depending on the type of data it uses. In an
array, similar types of elements are used. So, Java allows creating arrays using “new” operator only.
Syntax:
datatype name_of_array[] = new datatype[n];
where “n” is the size of the array.
Internally, arrays are the objects containing
1. A group of contiguous memory locations having the same name of the same data type but different memory
locations with different index values.
2. Since it is an object, the beginning address location of the array becomes the reference that is used to access the
array elements.
3. Initialisation can be done directly or taking values from the user.
Syntax:
i. name_of_array[index]=value; or
ii. name_of_array[]={value1, value2, value3,……};
There are eight data types in Java and all the different data types are used to create an array. Both static and dynamic
declarations can be done.
1. int array[]={2,4,6,8,10}; // static declaration
int array[]=new int[10]; // dynamic declaration
for(int i=0;i<10;i++)
{
System.out.println("Enter a number");
array[i]=sc.nextInt();
}
2. String array[]={"aa", "bb", "cc", "dd", "ee"}; // static declaration
String array[]=new String[5]; // dynamic declaration
for(int i=0;i<10;i++)
{
System.out.println("Enter a name");
array[i]=sc.next ();
}
Similarly, the arrays of different data types can be created.
9.2.1 Dynamic Array Declaration
Array Types Data Types Format
Integer array byte byte ar[]=new byte[10];
short short ar[]=new short[10];
long long ar[]=new long[10];
Real numeric array float float ar[]=new float[10];
double double ar[]=new double[10];
Character array char char ar[]=new char[10];
9.2.2 Static Array Declaration
1. double array[]={1.2, 3.4, 1.5, 76.4, 55.5};
2. char array[]={'a', 'B', '2', '5', ':'};
278278 Touchpad Computer Science-XII

