Professional Documents
Culture Documents
<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java
Written by
► A list of items can be given one variable name using only one subscript and such a variable is called
a single-subscripted variable or a one –dimensional array.
Creating an array
► Array must be declared and created in the computer memory before they are used.
► Creation of array involves three steps:
► declare the array
► create memory locations
► put values into the memory locations
Declaration of an Array
Array can be declared in two forms
Syntax:
1) Type arrayname [ ];
2) data type [ ]arrayname;
Example: int number [ ];
int [ ] numbers;
► After declaring the variable for the array, the array needs to be created in the memory.
► This can be done by using the new operator in the following way:
Arrayname = new type[size];
numbers = new int [10];
► This statement assigns ten contiguous memory locations of the type int to the variable numbers. The
array can store ten elements.
int number [] = new int[10];
The final step is to put values into the array created. This process is known as
initialization .
Syntax:
Arrayname[subscript] = value;
Example:
number[0] = 50;
number[1] = 60;
we can also initialize array automatically in the same way as ordinary variable
when they are declared.
type arrayname[] = { list of values};
int number[] = { 35,40,50,45,95 };
In java all array store the allocated size in a variable named length. We can access the length of the
array using a.length
int asize = a.length;