The definition of Java provides a data structure called an array that stores an ordered collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring variables directly such as number0, number1, ..., and number99, you can declare an array variable named numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent each individual variable.
Declaring Array Variables
To use an array in a program, you must declare a variable to represent the array, and you must specify the type of array that the variable will hold.
Syntax:
Declaring Arrays for Different Data Types:
The image below represents the array myList. Here, myList holds 10 double values, and the indices range from 0 to 9.
Source Code:
public class Array {
public static void main(String[] args) {
// Step 1: Declare and create an array
int[] numbers = new int[3];
// Step 2: Assign values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
// Step 3: Print array values
System.out.println("Array elements:");
System.out.println(numbers[0]);
System.out.println(numbers[1]);
System.out.println(numbers[2]);
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

