Array is like a variable with the only difference that it can store multiple values while variable can store only one value at a time. But the value should be of same data type like integer, character. The 0th index is the first element of array in java. The basic syntax for array declaration is: data-type[] array_name. A simple example would be double[] marks. Here marks is an array storing the value of data type double (for example: 1.233343, 3.565333, 4.564333 and so on).
Here's a simple array that stores integer value and I have written a code that prints all the integer value in the array using a while loop.
package Lesson1;
public class ArrayClass {
public static void main(String[] args) {
int[] integer_array = {10,20,24,32,47};
System.out.println(integer_array[2]);
System.out.println(integer_array.length);
System.out.println();
int index=0;
while(index < integer_array.length ) {
System.out.println(integer_array[index]);
index++;
}
}
}
The following is the output of the above code.