☕ Java
Arrays in Java
An array is how you store a list of values under one name. Instead of creating 100 separate variables for 100 exam scores, you create one array. Simple idea, but it unlocks a ton of power.
Single-Dimensional Arrays
An array holds multiple values of the same type, accessed by index. The index always starts at 0 — so the first element is at index 0, not 1. This trips up beginners constantly.
Think of it like seats in a row at a cinema — seat 0, seat 1, seat 2... The row has a fixed number of seats, and you access each by its number.
Java
// Declare and initialize
int[] scores = {85, 92, 78, 95, 88};
// Access elements — remember: 0-indexed!
System.out.println(scores[0]); // 85 — first element
System.out.println(scores[4]); // 88 — last element
System.out.println(scores.length); // 5 — total elements
// Common mistake: accessing scores[5] on a 5-element array
// → ArrayIndexOutOfBoundsException (the most common Java crash for beginners)
// Iterate with for-each
for (int score : scores) {
System.out.println(score);
}2D Arrays — Tables and Grids
A 2D array is an array of arrays — think of it as a spreadsheet grid, a chessboard, or a seating chart. You need two indices to access a value: row and column.
Java
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// matrix[row][column]
System.out.println(matrix[1][2]); // 6 — row 1, col 2
// Print the entire grid
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Output:
// 1 2 3
// 4 5 6
// 7 8 9Arrays Utility Class — Don't Reinvent the Wheel
Java's java.util.Arrays class gives you built-in methods for sorting, searching, copying, and printing arrays. There's almost never a reason to write your own sort from scratch.
Java
import java.util.Arrays;
int[] arr = {5, 2, 8, 1, 9, 3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 5, 8, 9]
// Binary search — only works on sorted arrays!
int idx = Arrays.binarySearch(arr, 8); // returns 4 (index of 8)
// Copy
int[] firstFour = Arrays.copyOf(arr, 4); // [1, 2, 3, 5]
// Fill an array with a value
int[] zeros = new int[5];
Arrays.fill(zeros, 0); // [0, 0, 0, 0, 0]Related Topics in Java Basics
Variables in Java
A variable is just a named box in memory that holds a value. Java is strict about what goes in each box — you tell it the type upfront. Once you get this, the rest of Java clicks into place.
Data Types in Java
Java needs to know exactly what kind of data it's dealing with before it can store or process it. Integers, decimals, characters, true/false — each has its own type. Knowing which to use (and why) makes your programs efficient and bug-free.
Operators in Java
Operators are the symbols that tell Java what to do with your data — add it, compare it, flip it, combine it. You use operators in literally every line of real code, so getting comfortable with them early pays off immediately.
Control Flow Statements
Without control flow, every program would just run from top to bottom in a straight line. Control flow lets your program make decisions, repeat actions, and skip code — the difference between a calculator and an actual application.