1 What is a Program?
Before we learn about arrays, we must understand the fundamentals. A program consists of two essential components:
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:
Store the actual value directly in memory
8 types in Java
Store the address (reference) to the actual data
Arrays, Strings, Objects
The 8 Primitive Data Types
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:
You need to store the ages of 100 students in your program. How would you do it?
100 variables! Hard to manage!
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?
Memory addresses are consecutive (contiguous)
Characteristics of Arrays
An array can only store elements of ONE data type. You cannot mix integers and strings in the same array.
Once an array is created, its size cannot be changed. You must know the size before creating it.
Elements are stored in adjacent memory locations, making access very efficient (using offset calculation).
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
Many students confuse these three steps. Understanding them is essential for Data Structures!
Creating a reference variable that will hold the address of the array.
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).
Allocating memory for the actual array using the new keyword.
Default Values: When an array is created, all elements are initialized to default values:
int,short,byte,long→ 0float,double→ 0.0boolean→ falsechar→ '\u0000' (null character)- Reference types → null
Assigning actual values to the array elements.
Shorthand Syntax
You can combine all three steps in one line:
6 Understanding Reference Variables
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:
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.
7 Pass by Value vs Pass by Reference
Understanding how data is passed to methods is crucial for writing correct programs.
A copy of the value is passed. Changes to the copy don't affect the original.
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
- Programs consist of instructions and data
- Data types: Primitive (8 types) vs Reference (arrays, objects)
- Arrays store multiple values of the same type in contiguous memory
- Three steps: Declaration → Creation → Initialization
- Reference variables hold addresses, not actual data
- Pass by Value copies values; Pass by Reference shares the same data
Arrays of arrays (like a table/matrix)
Arrays of 2D arrays (like a cube)
Rows with different lengths
Irregular multi-dimensional structures