🧱 Arrays · Beginner

Creating arrays in Java

new int[5], array initializers {1,2,3}, fixed size.

🧩 The mysteryYour game tracks 100 players' scores. Do you really want to declare score1, score2, ... score100? There's a better box.

One name, many slots

An array holds a fixed number of values of one type, all under one name. new int[5] creates five int slots, all starting at 0.

int[] scores = new int[5]; // 0,0,0,0,0
scores[0] = 90;
scores[4] = 75;

Two ways to create

Give a size (new int[5], filled with defaults), or list the values in an initializer ({1, 2, 3}), and the size is inferred from them. new int[]{1, 2, 3} works too. The C-style int c[] is also legal, just old-fashioned.

int[] a = new int[5];       // size 5
int[] b = {1, 2, 3};        // size 3
int[] c = new int[]{1, 2};  // size 2
String[] names = {"Ana", "Ben"};
🔮 Predict it

Your turn

What prints?

int[] a = new int[3];
a[2] = 9;
String out = a.length + " " + a[0];
System.out.println(out + " " + a[2]);
  1. 3 0 9
  2. 1 0 9
  3. 3 9 9
  4. 2 0 9
Show the answer

new int[3] makes three slots, all 0. Setting a[2] fills one slot but doesn't change the length, which stays 3. a[0] is still 0.

⚠️ The trap

Fixed forever

An array's size is fixed at creation. There's **no add method**: a.add(4) doesn't compile. To "grow" an array you must build a new, bigger one and copy (or use an ArrayList). The size may be a variable, new int[n], but it must not be negative.

int[] a = new int[3];
a.add(4);              // compile error
int[] bigger = new int[6]; // new array
🔮 Predict it

A negative size?

What happens?

int size = 2 - 5;
int[] a = new int[size];
System.out.println(a.length);
  1. Prints 0
  2. Prints 3
  3. Compile error
  4. Throws NegativeArraySizeException
Show the answer

The size is checked at runtime, when the array is created. 2 - 5 is -3, so Java throws NegativeArraySizeException. The compiler can't know the value of a variable.

Initializer rules

✗ Won't compile
int[] d = new int[3]{1, 2, 3};
int[] e;
e = {1, 2};

With values listed, the size comes from them: giving both is an error. And bare {...} only works in a declaration.

✓ Compiles
int[] d = new int[]{1, 2, 3};
int[] e;
e = new int[]{1, 2};

Size inferred from the values; later assignments spell out new int[].

💼 In the real world

In real projects

Arrays sit underneath much of Java. An ArrayList is really an array in disguise: when it fills up, it creates a bigger array and copies everything over. Images are arrays of pixels, and network and file buffers are byte arrays.

Key takeaways

  1. int[] a = new int[5]; gives five zeros
  2. int[] b = {1, 2, 3}; infers the size (3) from the values
  3. The size is fixed at creation and can't change
  4. The size may be a variable (new int[n]) but must not be negative

💡 An array is like an egg carton: you pick its size when you buy it, and you can't add a 13th cup later.

🤯 Did you know?

An array's length is an int, so no Java array can ever hold more than Integer.MAX_VALUE (2,147,483,647) elements.

Practice questions

What does this print?

int[] nums = new int[4];
nums[1] = 7;
System.out.println(nums.length + " " + nums[1]);
  1. 1 7
  2. 4 7
  3. 4 0
  4. Compile error
Check your answer

4 7. new int[4] makes four slots, all 0. Setting nums[1] doesn't change the length, which stays 4.

Which line does NOT compile?

  1. int[] a = {1, 2, 3};
  2. int[] b = new int[]{1, 2, 3};
  3. int c[] = new int[3];
  4. int[] d = new int[3]{1, 2, 3};
Check your answer

int[] d = new int[3]{1, 2, 3};. When you list initial values, the size comes from them, so writing a size as well is an error. (int c[] is legal, old C-style syntax.)

Next: the first element is number 0, not 1, and the length isn't a method. Two tiny rules, many crashes.