Arrays.fill and copyOf
Fill and resize arrays.
Arrays.fill and copyOf is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Filling Arrays
Arrays.fill sets every element of an array to the same value. It modifies the array in place and is handy for initializing or resetting data.
A Basic Fill
Fill an entire array with one value.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = new int[5];
Arrays.fill(nums, 7);
System.out.println(Arrays.toString(nums));
}
}Filling a Range
Arrays.fill(array, from, to, value) fills only part of the array. The range is inclusive of from and exclusive of to.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = new int[6];
Arrays.fill(nums, 2, 5, 9);
System.out.println(Arrays.toString(nums));
}
}Filling Object Arrays
Fill works on object arrays too. Note that every slot points to the same reference when you fill with an object.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] grid = new String[4];
Arrays.fill(grid, "empty");
System.out.println(Arrays.toString(grid));
}
}Resizing with copyOf
Arrays have a fixed length. To get a bigger or smaller array, Arrays.copyOf(array, newLength) returns a new array of the requested size.
Growing an Array
When the new length is larger, extra slots get the default value: 0 for numeric types, false for boolean, null for objects.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
int[] bigger = Arrays.copyOf(nums, 5);
System.out.println(Arrays.toString(bigger));
}
}Shrinking an Array
When the new length is smaller, the array is truncated and trailing elements are dropped.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
int[] smaller = Arrays.copyOf(nums, 3);
System.out.println(Arrays.toString(smaller));
}
}Copying a Range
Arrays.copyOfRange(array, from, to) copies a slice into a new array. Again from is inclusive and to is exclusive.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {10, 20, 30, 40, 50};
int[] middle = Arrays.copyOfRange(nums, 1, 4);
System.out.println(Arrays.toString(middle));
}
}copyOf Does Not Mutate
Unlike fill, copyOf leaves the original array untouched and returns a brand new array. The original and the copy are independent.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, 3);
copy[0] = 99;
System.out.println("Original: " + Arrays.toString(original));
System.out.println("Copy: " + Arrays.toString(copy));
}
}A Manual Resize Idiom
Before ArrayList, programs grew arrays by copying into a larger one. copyOf doubles the capacity in one call.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] data = {1, 2, 3, 4};
data = Arrays.copyOf(data, data.length * 2);
data[4] = 5;
System.out.println(Arrays.toString(data));
}
}fill vs copyOf
Remember the difference: fill overwrites existing slots in place and returns void; copyOf creates a resized copy and returns it. Choose based on whether you need a new length.
Quick Check
Test your understanding of fill and copyOf.
Recap
You learned to fill and resize arrays.
Arrays.fillsets all (or a range of) elements to one value, in place.Arrays.copyOfreturns a new array of a given length, padding or truncating.Arrays.copyOfRangecopies a slice.- copyOf is non-destructive; fill mutates.
Frequently asked questions
Is the “Arrays.fill and copyOf” lesson free?
Yes — the full text of “Arrays.fill and copyOf” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Arrays.fill and copyOf”?
Fill and resize arrays. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Arrays.fill and copyOf” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrays.sort and Sorting
- Arrays.binarySearch
- Arrays.fill and copyOf
- Arrays.equals and toString