JOOPs Through Java
Challenge roadmap
0/30 Days Completed 0 Learning Days

Day 10 of 30 · Week 2 — Building Java Basics

Arrays

1 Learn

An array stores many values of the same type under one name, like a row of numbered lockers.

Positions (indexes) start at 0. An array of 5 items has indexes 0 to 4. arr.length gives the size.

Using an index outside the range causes an ArrayIndexOutOfBoundsException.

Want more detail? Open the full lesson

2 See the Code

Java
public class Marks {
    public static void main(String[] args) {
        int[] marks = {78, 92, 65, 88};
        System.out.println("First: " + marks[0]);
        marks[2] = 70;
        int total = 0;
        for (int m : marks) {
            total += m;
        }
        System.out.println("Count: " + marks.length);
        System.out.println("Total: " + total);
    }
}
  • marks[0] is the first value, 78.
  • marks[2] = 70 replaces 65 with 70.
  • for (int m : marks) is the enhanced for loop — it visits each value.

Output

First: 78
Count: 4
Total: 328

3 Practice

  • Store 5 temperatures in an array and print the highest one.

Try it in your own Java setup or write it in the Practice Notebook.

4 Quick Check

  1. 1. What is the index of the first element?

  2. 2. An array has length 6. What is the last index?

Step 5

Answer and check the Quick Check above to unlock this.