Nearby lessons
51 of 125Java - 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
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
Two-dimensional and Jagged Arrays
Example04
📝 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 QuestionsProgress: 0 / 1