Array Declaration and Initialization
You'll learn how to create arrays and fill them with starting values using the syntax of different programming languages.
The Core Idea
Now that you know what an array is (a collection of elements stored together), you need to know how to create one. Declaration tells the computer "I want an array of this type and size," while initialization fills it with starting values.
Think of it like ordering a pill organizer: declaration is choosing how many compartments you need (7 days? 14?), and initialization is putting pills into each slot right away — or leaving them empty for now.
Declaration Syntax
Different languages have different ways to declare arrays:
Python:
numbers = [10, 20, 30, 40] # Create and initialize in one step
Java:
int[] numbers = new int[4]; // Declare array of 4 integers (all start at 0)
C:
int numbers[4]; // Declare array of 4 integers (uninitialized)
Initialization Options
You can initialize arrays in two main ways:
-
With values immediately:
- Provide the exact elements:
[10, 20, 30, 40]
- Provide the exact elements:
-
Empty, specifying only size:
- Most languages fill with default values (like 0 for integers)
- You'll add values later using assignment
Some languages require you to declare the size upfront; others (like Python) let the list of values determine the size automatically.
Key Takeaway:
Array declaration reserves space for multiple elements of the same type, and initialization sets their starting values — either all at once or with defaults to be filled later.