Arrays
An array is a fixed-size collection that stores multiple values of the same type together.
Every value has its own "index", starting at 0 — the first element is at index 0, not 1!
int[] scores = {90, 85, 77, 60};
System.out.println(scores[0]); // 90- Fixed-size row of same-type values
- Index starts at 0
- array[i] accesses a value
Arrays can be built several ways: with a size (new int[5], every value gets the default 0 — false for booleans, null for objects), or directly with values ({90, 85, 77}, only works at the moment of declaration), or with new and values together (new int[]{90, 85, 77}, usable anywhere, like as a method argument).
Once an array's size is decided, it's fixed — array.length tells you the size (it's a field, not a method — written without (), don't confuse it with String's length()).
int[] a = new int[5]; // sab 0
int[] b = {90, 85, 77}; // direct values
int[] c = new int[]{90, 85, 77}; // explicit new
System.out.println(b.length); // 3 (property, no parentheses)Besides primitives, an array can store objects too — like String[] or Student[]. Remember: creating an object array (new Student[3]) only creates 3 "empty slots" (references), not actual objects — each element must be individually built with 'new', or it stays null.
This is why NullPointerException is common when working with object arrays, if you forget to initialize every slot.
Student[] students = new Student[3];
// students[0] abhi null hai!
students[0] = new Student("Aarav"); // har object alag se banana padta hai
System.out.println(students[1]); // null (abhi tak banaya nahi)The java.util.Arrays class has many useful static methods that make working with arrays easier. Arrays.sort() sorts in ascending order (in-place, changing the original array). Arrays.binarySearch() searches a sorted array very fast (O(log n)) — but the array *must* be sorted, otherwise it can give a wrong result.
Arrays.fill() fills all (or some) elements with one value. Arrays.toString() gives a readable print (otherwise println(array) just shows a memory address!). Arrays.copyOf() makes a new, bigger/smaller array with the old data.
| Method | What It Does |
|---|---|
| Arrays.sort(arr) | Sort in ascending order (in-place) |
| Arrays.binarySearch(arr, x) | Fast search on a sorted array |
| Arrays.fill(arr, x) | Fill all elements with x |
| Arrays.toString(arr) | Builds a readable String for printing |
| Arrays.copyOf(arr, n) | A new array, of size n |
| Arrays.equals(a, b) | Are two arrays equal (by content) |
int[] nums = {5, 2, 8, 1};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // [1, 2, 5, 8]An array's size is fixed — once created, you can't grow or shrink it. If you need more room, you'll have to create a NEW, bigger array and copy the old data over (which is exactly what ArrayList does internally for you, automatically!).
That's why, when you need a dynamic size (adding/removing elements over time, size unknown upfront), you use a Collection like ArrayList — it manages an internal array and resizes it for you, so you don't have to do it manually. An array is best when the size is fixed and known upfront (better performance, less memory overhead).