
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an Array of Objects in Java
In this article, we will learn to create an array of objects in Java. An array of Object classes can be created that can accept any type of object. During the operation on such an array, instanceof operator can be used.
Different Approaches
The following are the two different approaches to creating an array of objects in Java ?
Using an Object[] Array
Java provides a built-in Object class, which is the superclass of all classes. This means an array of objects can hold values of different types, including primitives wrapped as objects.
Following are the steps to store different data types using Object[] ?
- We declare an Object[] array with a fixed size of 3.
- We store different types of objects: Integer, String, and Boolean.
- We use instanceof to check the actual type of each object during iteration.
- Each object is cast to its respective type before calling its methods.
Object[] dataArray = new Object[3]; dataArray[0] = new Integer(0); dataArray[1] = new String("1"); dataArray[2] = new Boolean(false);
Example
Below is an example of storing different data types using Object[] ?
public class Tester { public static void main(String[] args) { Object[] dataArray = new Object[3]; dataArray[0] = Integer.valueOf(0); dataArray[1] = "1"; dataArray[2] = Boolean.valueOf(false); for (Object data : dataArray) { if (data instanceof Integer) { System.out.println((Integer) data); } else if (data instanceof String) { System.out.println(data); } else if (data instanceof Boolean) { System.out.println((Boolean) data); } } } }
Output
0 1 false
Time Complexity: O(n), Iterating through the array.
Space Complexity: O(n), storing n objects.
Using a Class-Specific Object Array
A better way to store and retrieve objects is by using an array of a specific class type, ensuring type safety and avoiding explicit type casting.
Following are the steps for storing Objects of a custom class in an array ?
- Defines an Employee class with name, id, and a display() method.
- Creates an Employee[] array, and initializes objects (Alice, Bob, Charlie).
- Iterates using a for-each loop to print employee details.
Defining an Employee class with name and id attributes ?
String name; int id;
The constructor initializes these attributes ?
Employee(String name, int id) { this.name = name; this.id = id; }
Example
Below is an example of storing Objects of a custom class in an array ?
class Employee { String name; int id; Employee(String name, int id) { this.name = name; this.id = id; } void display() { System.out.println("Employee ID: " + id + ", Name: " + name); } } public class ObjectArrayExample { public static void main(String[] args) { Employee[] employees = new Employee[3]; employees[0] = new Employee("Alice", 101); employees[1] = new Employee("Bob", 102); employees[2] = new Employee("Charlie", 103); for (Employee emp : employees) { emp.display(); } } }
Output
Employee ID: 101, Name: Alice Employee ID: 102, Name: Bob Employee ID: 103, Name: Charlie
Time Complexity: O(n), iterating and calling display () method.
Space Complexity: O(n), storing n Employee objects.
Conclusion
Both methods enable defining an array of objects in Java, yet they have various uses. Object[] Array offers flexibility to store varying types of data but needs type checking and casting. Class-Specific Object Array is type-safe, prevents explicit casting, and is the better choice when working with particular object types.