6.1 pre calc
6.1 pre calc
• You can store a value in an array using an index (location in the array).
• The declarations do not create an array. Arrays are objects in Java, so any variable that
declares an array holds a reference to an object.
• If the array hasn’t been created yet and you try to print the value of the variable, it will
print null (meaning it doesn’t reference any object yet).
• There are two ways to create an array. You can use the keyword new to get new memory or
use an initializer list to set up the values in the array.
1. Using the new Keyword
This method dynamically allocates memory for the array. You define the size of the array
and optionally initialize its elements later.
Example:
Key Points:
• Memory for the array is dynamically allocated.
• The size must be specified at the time of creation.
• All elements are initialized to default values (e.g., 0 for int, null for objects).
Now, go to Runestone Academy and complete all the activities in section 6.1.2.
2. Using an Initializer List
This method allows you to directly specify the array's contents using curly braces {}. The
size of the array is implicitly determined by the number of elements provided.
Syntax:
Example:
Key Points:
• No need to specify the size explicitly.
• Ideal when you know the initial values at the time of creation.
• The array is created and populated in a single step.
Example:
Explanation:
• Each element in the array stores a value of type int (a primitive type).
• The values (99, 98, etc.) are stored directly in contiguous memory locations.
• The indices (0, 1, 2, ...) are used to access these values.
Object Reference Array for example String[ ] names:
• What it does: Stores references (pointers) to objects, not the objects themselves.
Example:
Explanation:
• Each element in the array stores a reference (like an address) to a String object.
• The actual String objects (e.g., "Jamal", "Emily", etc.) are stored elsewhere in memory.
• The references in the array point to where these objects are located.
• This is because String is not a primitive type, it's an object type.
Why is "Jamal" a Reference in the Array?
In Java, a String is an object (not a primitive type like int or boolean), and objects are always
referenced in memory. This means:
• The names array itself does not store "Jamal"'s data directly (like "J", "a", "m", etc.).
• Instead, it stores a reference to the memory location where the "Jamal" object exists in
the String Literal Pool.
Think of the names array as a list of pointers:
• names[0] points to the "Jamal" object.
• names[1] points to the "Emily" object.
• And so on.
Final Note
So, "Jamal" is indeed a String. The names array doesn't directly store the "Jamal" data; it stores
a reference to the "Jamal" object in memory.
Simplified Analogy
• Primitive Arrays: Imagine a row of boxes, each containing a number directly (e.g., 99, 98,
68).
• Object Reference Arrays: Imagine a row of boxes, but instead of numbers, the boxes
contain pointers to people in a nearby room (e.g., Jamal, Emily, Sofia). The box doesn't
hold the person, just the address.
Task: Understanding Array Creation in Java
Instructions:
1. Part 1: Create an Array Using the new Keyword
o Create an array named grades that can store 5 integers.
o Initialize the array using the new keyword and assign the following values to it:
85, 90, 78, 92, 88.
o Print all the elements of the grades array using a loop.
2. Part 2: Create an Array Using an Initializer List
o Create an array named subjects that stores the names of 4 subjects: "Math",
"Science", "English", "History".
o Print all the elements of the subjects array using a loop.
3. Part 3: Analyze and Modify Arrays
o Modify the first element of the grades array to 95 and the last element of the
subjects array to "Art".
o Print the updated arrays to confirm the changes.
Now, go to Runestone Academy and complete all the activities in section 6.1.5
If you've tried enough and still can't figure out how to solve it, you can use ChatGPT—but only
after you've run out of attempts.
6.1.6. Programming Challenge: Countries’ Array
https://www.programiz.com/online-compiler/6kNv37wxDAAOp
Now, go to Runestone Academy and complete all the activities in section 6.1.7
If you've tried enough and still can't figure out how to solve it, you can use ChatGPT—but only
after you've run out of attempts.