C

Core Java tutorial for beginners

Clean • Professional

Core Java Arrays – Arrays of Objects in Java

3 minute

Arrays of Objects in Java

In Java, you can create arrays not only for primitive types (like int, double) but also for objects, including user-defined classes. Arrays of objects are widely used to store multiple instances of a class in a single container, making data management efficient.


Why Use Arrays of Objects?

  • Organize related objects in a single container.
  • Efficient for storing multiple user-defined types.
  • Useful in real-world applications, such as:
    • School management → array of Student objects.
    • Employee management → array of Employee objects.
    • Inventory systems → array of Product objects.

Declaring and Creating Arrays of Objects

learn code with durgesh images

Step 1: Declare the Array

Student[] students; // Declares an array of Student objects

Step 2: Allocate Memory

students = new Student[3]; // Array can hold 3 Student references

At this point, students[0], students[1], students[2] are null because no objects have been created yet.

Step 3: Initialize Objects

class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println(id + " " + name);
    }
}

public class ArrayOfObjectsExample {
    public static void main(String[] args) {
        Student[] students = new Student[3];

        students[0] = new Student(1, "Aman");
        students[1] = new Student(2, "Neha");
        students[2] = new Student(3, "Ravi");

        for (Student s : students) {
            s.display();
        }
    }
}

Output:

1 Aman
2 Neha
3 Ravi

Points to Remember

  • Arrays store references, not the actual objects.
  • Always instantiate each object before use to avoid NullPointerException.
  • Use array.length to get the number of object references.
  • Arrays of objects can be multidimensional:
Student[][] classes = new Student[2][3];
  • For-each loop provides clean iteration syntax:
for (Student s : students) {
    s.display();
}

Polymorphism with Arrays of Objects

You can store subclass objects in a superclass array:

class Animal { void sound() { System.out.println("Some sound"); } }
class Dog extends Animal { void sound() { System.out.println("Woof"); } }
class Cat extends Animal { void sound() { System.out.println("Meow"); } }

public class PolymorphismArrayExample {
    public static void main(String[] args) {
        Animal[] animals = new Animal[2];
        animals[0] = new Dog();
        animals[1] = new Cat();

        for (Animal a : animals) {
            a.sound(); // Runtime polymorphism
        }
    }
}

Output:

Woof
Meow

This demonstrates runtime polymorphism using arrays of objects.


Common Operations on Arrays of Objects

OperationExample
Access object fieldsstudents[0].name
Invoke methodsstudents[1].display()
Iterate arrayfor(Student s : students) { s.display(); }
Sorting objectsUse Arrays.sort() with Comparable/Comparator
Copying object referencesUse loops or System.arraycopy()

Points to Remember:

  • Arrays of objects store references, not objects themselves.
  • Each object must be instantiated before usage.
  • Supports polymorphism, methods, sorting, and searching like primitive arrays.
  • Can be combined with Collections for dynamic sizing.

🏆 Top 5 Interview Questions – Arrays of Objects in Java

1. How do you declare and initialize an array of objects?

  • Declaration:
Student[] students;
  • Memory allocation:
students = new Student[3]; // Holds references, all are null initially
  • Initialization:
students[0] = new Student(1, "Aman");

2. Can arrays store different object types?

Answer: Yes, using superclass references (polymorphism):

Animal[] animals = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
  • Allows runtime polymorphism when calling overridden methods.

3. How do you iterate over an array of objects?

Answer:

  • Using for loop:
for (int i = 0; i < students.length; i++) {
    students[i].display();
}
  • Using for-each loop:
for (Student s : students) {
    s.display();
}

4. How do arrays of objects differ from primitive arrays?

Answer:

FeaturePrimitive ArrayArray of Objects
StoresActual valuesReferences to objects
Default0, false, '\0'null
OperationsDirect value manipulationMust instantiate objects first

5. How can you sort or copy arrays of objects?

Answer: Sorting: Use Arrays.sort() with Comparable or Comparator:

Arrays.sort(students, (s1, s2) -> s1.id - s2.id);
  • Copying references: Use loops or System.arraycopy():

Student[] copy = new Student[students.length];
System.arraycopy(students, 0, copy, 0, students.length);

 

Article 0 of 0