Creating arrays in Java
new int[5], array initializers {1,2,3}, fixed size.
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"};Your turn
What prints?
int[] a = new int[3];
a[2] = 9;
String out = a.length + " " + a[0];
System.out.println(out + " " + a[2]);3 0 91 0 93 9 92 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.
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 arrayA negative size?
What happens?
int size = 2 - 5;
int[] a = new int[size];
System.out.println(a.length);Prints 0Prints 3Compile errorThrows 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
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.
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 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
- int[] a = new int[5]; gives five zeros
- int[] b = {1, 2, 3}; infers the size (3) from the values
- The size is fixed at creation and can't change
- 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.
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 7
- 4 7
- 4 0
- 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?
- int[] a = {1, 2, 3};
- int[] b = new int[]{1, 2, 3};
- int c[] = new int[3];
- 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.)