Skip to content
C

Arrays

An array is a single container that can hold multiple values of the same data type, all stored together under one variable name. Each value inside the array is accessed using a position number called an index.


1. What is an Array?

An array is a single container that can hold multiple values of the same data type, all stored together under one variable name. Each value inside the array is accessed using a position number called an index.

2. Why is it used?

Without arrays, storing many related values (like 50 students' marks) would mean creating 50 separate variables — completely impractical. An array lets you manage all of them together, using one name and simple index numbers.

3. Real-Life Example

Think of an egg tray with 12 slots. Instead of keeping 12 separate boxes for 12 eggs, the tray holds all of them together, and each egg has a fixed position in the tray. An array works the same way for data.

4. Syntax

java
dataType[] arrayName = new dataType[size];

5. Example Program

java
public class ArrayDemo { public static void main(String[] args) { int[] marks = new int[3]; marks[0] = 80; marks[1] = 90; marks[2] = 70; System.out.println("First mark: " + marks[0]); } }

Output:

First mark: 80

6. Key Points to Remember

  • Array indexing starts from 0, not 1 — the first element is at index 0.
  • Once created, an array's size cannot be changed.
  • All elements in an array must be of the same data type.