Array of Objects in Java — Java Technical Interview Questions & Answers
Array of Objects in Java
Array of Objects in Java
- Array of Objects is about Java arrays. An array is a fixed-size row of the same type. Index from 0. Length is a.length (a field, not a method). Last valid index is length - 1.
- Create Array of Objects: int[] a = new int[5]; or int[] a = {10, 20, 30};. A 2D array is an array of arrays: int[][] m = {{1,2},{3,4}}; m[1][0] is 3. Jagged means rows can have different lengths.
- Array of Objects size cannot grow. If you need grow/shrink, use ArrayList. Out of range → ArrayIndexOutOfBoundsException.
- Let's take this on the board with one tiny Main class — no extra files. Array of Objects in Java — prints length 3, middle value 20, then all values. Start from main, go line by line, and stop at each print. That output is the proof for Array of Objects.
index → 0 1 2
array → [10, 20, 30]
↑ ↑
first length-1Technical Interview Tip: Contrast array vs ArrayList in one line.
Code Examples for Array of Objects in Java
Example
// Array demo
public class Main {
public static void main(String[] args) {
int[] nums = {10, 20, 30};
System.out.println(nums.length);
System.out.println(nums[1]);
for (int n : nums) System.out.print(n + " ");
}
}Array of Objects in Java — prints length 3, middle value 20, then all values.
How to Explain Array of Objects in Java to an Interviewer
What is Array of Objects?
Array of Objects — array = fixed-size, same type, index from 0. Array of Objects — a.length is a field. Last index = length - 1. Array of Objects — 2D = array of arrays. Jagged = rows of different length.
Why do we use Array of Objects in Java?
Makes the program clearer or safer Array of Objects — array = fixed-size, same type, index from 0.
Write one small example of Array of Objects.
Show syntax Say what prints
What mistake do beginners make with Array of Objects?
Only the name, no example Array of Objects — a[a.length] crashes. Size cannot grow — use ArrayList.
Where is Array of Objects used in a real program?
Day-to-day Java code