Declaring & Initializing
Arrays in Java
Master the fundamentals of declaring, allocating, and initializing arrays: the backbone of organized data in Java.
What Is an Array?
An array is a fixed-size, ordered collection of elements that share the same data type. Think of it as a row of numbered boxes, each holding one value. You access each box by its index: an integer starting at 0.
Why Use Arrays?
Without arrays, storing 100 test scores requires 100 separate variables. Arrays let you hold all values in a single named structure and access any element instantly by index.
for loop rather than repetitive code.Key Vocabulary
arr.length (no parentheses).The Three Steps to Create an Array
int[] scores;new and specify the size: scores = new int[5];scores[0] = 95; or use a shortcut literal.Declaration & Allocation Syntax
Default Values After Allocation
When you allocate an array with new, Java automatically fills every slot with a default value based on the element type:
| Element Type | Default Value |
|---|---|
| int, short, byte, long | 0 |
| double, float | 0.0 |
| boolean | false |
| char | '\u0000' |
| Object references (String, etc.) | null |
Two Ways to Initialize
new, then assign each element individually. Best when values are computed at runtime.// Approach 1: new + element-by-element int[] ages = new int[3]; ages[0] = 15; ages[1] = 17; ages[2] = 16; // Approach 2: Initializer literal int[] agesToo = {15, 17, 16};
How Arrays Live in Memory
How to Apply It: Step-by-Step
int, double, String, etc.)type[] name = new type[size]; or use the initializer literal type[] name = {values};name.length.name[index] to read or write. Always stay within 0 to \( \text{length} - 1 \).Example 1: Storing Test Scores
// Declare + allocate an array of 4 scores int[] scores = new int[4]; // Initialize each element scores[0] = 92; scores[1] = 85; scores[2] = 78; scores[3] = 96; System.out.println(scores[2]); // Output: 78 System.out.println(scores.length); // Output: 4
Example 2: String Array Literal
// Initializer literal: size is inferred as 4 String[] days = {"Mon", "Tue", "Wed", "Thu"}; System.out.println(days[0]); // "Mon" System.out.println(days.length); // 4 // Overwrite an element days[3] = "Fri"; System.out.println(days[3]); // "Fri"
Example 3: Looping Through an Array
double[] temps = {72.5, 68.3, 74.1, 69.8, 71.0}; double sum = 0; for (int i = 0; i < temps.length; i++) { sum += temps[i]; } double avg = sum / temps.length; System.out.println(avg); // 71.14
Common Mistakes
ArrayIndexOutOfBoundsException at the last iteration.int[] arr; only declares a reference. If you try to use it before initializing (e.g., arr[0] = 5;), you'll get a compile-time error.int[3] arr = {1,2,3}; is illegal syntax. With an initializer literal, do not put a number in the brackets.Summary Comparison
| Feature | new + Assignment | Initializer Literal |
|---|---|---|
| Syntax | int[] a = new int[n]; |
int[] a = {1, 2, 3}; |
| Size specified? | Explicitly | Implicitly (counted) |
| Initial values | Defaults (0, null, etc.) | Your supplied values |
| Best for | Dynamic / computed data | Known constants |
| Can reassign later? | Elements: yes. Size: no | Elements: yes. Size: no |
Question 1 of 5
Which declaration correctly creates an array of 10 doubles in Java?
[] after the type (or after the name), uses new, and specifies the size inside the new expression: double[] arr = new double[10];Question 2 of 5
After the line int[] arr = new int[5]; executes, what is the value of arr[3]?
int array to 0 by default. There are no garbage values in Java arrays.Question 3 of 5
Given: String[] colors = {"red", "green", "blue"};
What does colors.length return?
length is a field (no parentheses) for arrays, unlike String.length() which is a method.Question 4 of 5
Which code will cause an ArrayIndexOutOfBoundsException at runtime?
0, 1, 2. Accessing index 3 is out of bounds, which throws ArrayIndexOutOfBoundsException at runtime.Question 5 of 5
What is the output of the following code?
int[] nums = {4, 8, 15, 16}; nums[1] = nums[3] + 2; System.out.println(nums[1]);
nums[3] is 16. Adding 2 gives 18, which is stored in nums[1], replacing the original value of 8. The output is 18.