Page 364 - CA_Blue( J )_Class10
P. 364
From the above example, it is understood that both the arrays declared are containing 40 items of respective data
type. In the first position, it will contain the name and roll number of student 1 in names[0] and roll_no[0]. In the
second position, it will contain the name and roll number of student 2 in names[1] and roll_no[1], and so on. Also, it
is proved from the figure that array occupies contiguous memory location as all the elements are created one after
another. You can also notice that the array index is started from 0 and the last index is 39 which means that you can
store values upto one index less than the size of the array.
Let us declare arrays of different data types:
Array Types Data Types Format
byte byte ar[]=new byte[10];
short short ar[]=new short[10];
Integer array
int int ar[]=new int[10];
long long ar[]=new long[10];
float float ar[]=new float[10];
Real numeric array
double double ar[]=new double[10];
char char ar[]=new char[10];
Character array
String String ar[]=new String[10];
Initializing an Array
After declaring an array, the next step is to initialize it. Initializing an array means to assign the values to it. There are
two different ways to initialize an array. Let us discuss about them.
Assigning Values Directly
Using the assignment operator, you can directly assign the values to an array at the time declaration or after declaration.
In this case, you do not need to give the size of the array as Java automatically identify the size of the array by counting
the values. Following is the syntax to initialize a single dimensional array:
name_of_array[] = {value1, value2,…..};
For example:
//Integer array
int ar[] = {22345, 455567, 6345, 8245, 12450};
byte ar[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10};
short ar[] = {111, 222, 333, 444, 555};
long ar[] = {123456, 23456, 76578345, 67567866};
//floating-point array
float ar[]={12.45f, 234.12f, 45.56f, 11.33f, 78.76f};
double ar[]={1111.22, 2234.44, 567.09, 56.33 };
// character array
char ar[]={'a','2',':','A'};
Sring ar[]={"India", "12345", "a123", "Class 10"};
362362 Touchpad Computer Applications-X

