Nearby lessons

51 of 125

Java - Arrays

📌 What You Will Learn
  • The small building blocks of Java: tokens
  • All 8 primitive data types with their ranges
  • Type casting — implicit and explicit
  • Control statements: if, switch, loops
  • Arrays — single, double and jagged
  • Variable length arguments (var-args)

Arrays is a core concept of the Java language. This lesson explains Arrays, Important points about arrays and Two-dimensional and Jagged Arrays with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Arrays

An array is a collection of same-type values stored one after another in memory. It is like a row of lockers — each locker (index) holds one value. Arrays have a fixed size once created.

Example01
JCode Cell
1int[] marks = new int[5]; // array of 5 ints, indexes 0 to 4
2marks[0] = 90; // first position
3marks[1] = 85;
4System.out.println(marks[0]); // 90
5 
6int[] numbers = {10, 20, 30, 40}; // short way - declare and fill
7System.out.println(numbers.length); // 4 (length is a property, not a method)

Important points about arrays

  • Index starts from 0, not 1. Last index is length - 1.
  • Array size cannot be changed after creation.
  • length is a property of the array (no brackets). String.length() is a method (with brackets). Do not mix them.
  • If you access an index outside the range, you get ArrayIndexOutOfBoundsException.
  • In Java, an array is actually an object.
In simple words: An array is a fixed-size row of lockers where the index starts at 0. The first value sits at index 0, the last at length - 1, and you cannot change the size after creation.

Two-dimensional and Jagged Arrays

A 2D array is like a table (rows and columns). We declare it with two sets of brackets.

A jagged array is a 2D array where each row can have a different number of columns.

Example03
JCode Cell
1int[][] matrix = { {1, 2, 3}, {4, 5, 6} }; // 2 rows, 3 columns
2System.out.println(matrix[1][2]); // 6
3 
4// Nested loops to print the whole table
5for (int[] row : matrix) {
6 for (int value : row) {
7 System.out.print(value + " ");
8 }
9 System.out.println();
10}
Output
1 2 3 4 5 6

Two-dimensional and Jagged Arrays

Example04
JCode Cell
1int[][] jagged = new int[3][];
2jagged[0] = new int[1]; // row 0 has 1 column
3jagged[1] = new int[2]; // row 1 has 2 columns
4jagged[2] = new int[3]; // row 2 has 3 columns
📝 Key Takeaways
  • Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
  • Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
  • Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
  • if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
  • Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
  • Var-args (int... x) lets a method take any number of arguments.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1