Introduction to Arrays in Java

Understanding Memory, Data Types, and Array Fundamentals

CPCS203 - Object-Oriented Programming | Week 1

1 What is a Program?

Before we learn about arrays, we must understand the fundamentals. A program consists of two essential components:

Components of a Program
Instructions
What to do (Code)
+
Data
What to work with
Key Question: Where is Data Stored?

Data is stored in the memory (RAM). But how exactly is it stored? This leads us to understanding data types.

2 Data Types in Java

In Java, data can be stored in two categories of data types:

Primitive Data Types

Store the actual value directly in memory

8 types in Java

Reference Data Types

Store the address (reference) to the actual data

Arrays, Strings, Objects

The 8 Primitive Data Types

byte
1 byte (8 bits)
-128 to 127
short
2 bytes (16 bits)
-32,768 to 32,767
int
4 bytes (32 bits)
-2.1B to 2.1B
long
8 bytes (64 bits)
Very large numbers
float
4 bytes (32 bits)
3.14f
double
8 bytes (64 bits)
3.14159265359
char
2 bytes (16 bits)
'A', 'B', '1'
boolean
1 bit
true / false
// Examples of primitive data types int age = 20; // Stores: 20 double gpa = 3.75; // Stores: 3.75 char grade = 'A'; // Stores: 'A' boolean isPassed = true; // Stores: true

What about String?

String is NOT a primitive data type! It is a reference type.

We will discuss this in detail in the coming weeks.

3 Why Do We Need Arrays?

Let's consider a real scenario:

Scenario: Store Student Ages

You need to store the ages of 100 students in your program. How would you do it?

Without Arrays
int age1 = 20; int age2 = 21; int age3 = 19; int age4 = 22; // ... 96 more lines! int age100 = 20;

100 variables! Hard to manage!

With Arrays
int[] ages; ages = new int[100]; // One variable to manage // all 100 ages!

One handle for 100 values!

Solution: Arrays!

An array is a single variable that can store multiple values of the same type in contiguous memory locations.

4 What is an Array?

Array: A Series of Contiguous Memory Locations
[0]
20
[1]
21
[2]
19
[3]
22
[4]
20

Memory addresses are consecutive (contiguous)

Characteristics of Arrays

1
Homogeneous (Same Type)

An array can only store elements of ONE data type. You cannot mix integers and strings in the same array.

2
Fixed Size

Once an array is created, its size cannot be changed. You must know the size before creating it.

3
Contiguous Memory

Elements are stored in adjacent memory locations, making access very efficient (using offset calculation).

4
Zero-Based Indexing

The first element is at index 0, not 1. An array of size 5 has indices 0, 1, 2, 3, 4.

Why is contiguous memory efficient?

When you access ages[3], the computer calculates:

address = base_address + (index × size_of_element)

You will learn more about this in Operating Systems (Memory Management).

5 The Three Essential Steps

Critical Understanding

Many students confuse these three steps. Understanding them is essential for Data Structures!

1
Declaration

Creating a reference variable that will hold the address of the array.

int[] ages; // Declares a reference variable
After Declaration: Reference Variable Created
Stack Memory
ages
null
int[] (reference)
Heap Memory
No array created yet
Important!

The reference variable ages does NOT contain the array data! It only holds the address (reference) to where the array will be stored. Currently, it holds null (nothing).

2
Creation (Instantiation)

Allocating memory for the actual array using the new keyword.

ages = new int[5]; // Creates array of 5 integers
After Creation: Array Allocated in Heap Memory
Stack Memory
ages
0x7A3F
int[] (reference)
Heap Memory (Address: 0x7A3F)
[0]
0
[1]
0
[2]
0
[3]
0
[4]
0

Default Values: When an array is created, all elements are initialized to default values:

  • int, short, byte, long0
  • float, double0.0
  • booleanfalse
  • char'\u0000' (null character)
  • Reference types → null
3
Initialization

Assigning actual values to the array elements.

ages[0] = 20; ages[1] = 21; ages[2] = 19; ages[3] = 22; ages[4] = 20;
After Initialization: Array Contains Our Values
Stack Memory
ages
0x7A3F
int[] (reference)
Heap Memory (Address: 0x7A3F)
[0]
20
[1]
21
[2]
19
[3]
22
[4]
20

Shorthand Syntax

You can combine all three steps in one line:

// Method 1: Declaration + Creation + Initialization int[] ages = new int[] { 20, 21, 19, 22, 20 }; // Method 2: Even shorter (array literal) int[] ages = { 20, 21, 19, 22, 20 };

6 Understanding Reference Variables

Critical for Data Structures!

Understanding reference variables is absolutely essential for your Data Structures course. Pay close attention to this section!

Let's see what happens when we assign one array variable to another:

int[] ages = { 20, 21, 19 }; int[] scores = ages; // What happens here?
Two References, ONE Array!
Stack Memory
ages
0x7A3F
scores
0x7A3F
Heap Memory (0x7A3F)
[0]
20
[1]
21
[2]
19
Key Understanding

Both ages and scores point to the SAME array in memory! If you change scores[0], the value of ages[0] also changes because they reference the same data.

scores[0] = 100; System.out.println(ages[0]); // Output: 100 (not 20!) System.out.println(scores[0]); // Output: 100

7 Pass by Value vs Pass by Reference

Understanding how data is passed to methods is crucial for writing correct programs.

Pass by Value (Primitives)
public class Test { public static void changeAge(int x) { x = 100; } public static void main(String[] args) { int age = 20; changeAge(age); System.out.println(age); // Output: 20 } }
Original
age = 20
Copy (in method)
x = 20 → 100

A copy of the value is passed. Changes to the copy don't affect the original.

Pass by Reference (Arrays)
public class Test { public static void changeArray(int[] arr) { arr[0] = 100; } public static void main(String[] args) { int[] ages = {20, 21}; changeArray(ages); System.out.println(ages[0]); // Output: 100 } }
ages (reference)
0x7A3F
arr (copy of ref)
0x7A3F
↓ Both point to ↓
[100, 21]

The reference (address) is copied. Both point to the same array!

Key Takeaway:

  • Primitives: The actual value is copied. Original is safe.
  • Arrays/Objects: The reference is copied. Both variables access the same data!

8 Summary & What's Next

Key Takeaways from Today
  1. Programs consist of instructions and data
  2. Data types: Primitive (8 types) vs Reference (arrays, objects)
  3. Arrays store multiple values of the same type in contiguous memory
  4. Three steps: DeclarationCreationInitialization
  5. Reference variables hold addresses, not actual data
  6. Pass by Value copies values; Pass by Reference shares the same data
Coming Next: Multi-Dimensional Arrays
2D Arrays

Arrays of arrays (like a table/matrix)

3D Arrays

Arrays of 2D arrays (like a cube)

Ragged 2D Arrays

Rows with different lengths

Ragged 3D Arrays

Irregular multi-dimensional structures