1. Arrays in Java
Definition
An array in Java is a data structure that allows you to store multiple values of the same data type in a single variable. It provides a way to organize data in a linear format.
Characteristics
- Fixed Size: Once an array is created, its size cannot be changed. You must define the size at the time of array creation.
- Zero-Based Indexing: Array indices start at 0, meaning the first element is accessed with index 0.
- Homogeneous Elements: All elements in an array must be of the same data type.
Declaration and Initialization
You can declare and initialize arrays in Java using the following syntax:
// Declaration
int[] numbers; // Preferred
int numbers[]; // Also valid
// Initialization
numbers = new int[5]; // Creates an array of size 5
// Combined Declaration and Initialization
int[] numbers = {1, 2, 3, 4, 5}; // Declares and initializes an arrayAccessing Array Elements
You can access array elements using their index:
int firstElement = numbers[0]; // Accessing the first element
numbers[1] = 10; // Modifying the second element2. Arrays of Objects
Definition
An array of objects is a collection of references to objects of a specific class. Each element of the array holds a reference to an object rather than the object itself.
Characteristics
- Reference Type: Each element in the array holds a reference to an object of a class, not the object itself.
- Fixed Size: Like regular arrays, the size of an array of objects is defined at the time of creation and cannot be changed.
- Heterogeneous Types: If you are using a base class, you can store objects of derived classes in the same array.
Declaration and Initialization
You can declare and initialize arrays of objects as follows:
// Define a simple class
class Student {
String name;
Student(String name) {
this.name = name;
}
}
// Declare and initialize an array of Student objects
Student[] people = new Student[3]; // Creates an array of size 3
people[0] = new Student("Alice");
people[1] = new Student("Bob");
people[2] = new Student("Charlie");Accessing Object Properties
You can access object properties and methods through the array:
String firstName = people[0].name; // Accessing the name property of the first Student object3. Advantages of Arrays
- Efficiency: Arrays allow for efficient access to elements using indexing, which has a constant time complexity (O(1)).
- Ease of Use: Arrays are easy to declare and initialize, making them simple to work with.
- Memory Allocation: Arrays are stored in contiguous memory locations, which can improve performance due to better cache locality.
4. Disadvantages of Arrays
- Fixed Size: The size of an array must be defined at creation, which can lead to wasted space or overflow.
- Homogeneous Elements: Arrays can only hold elements of the same data type (or type hierarchy in the case of object arrays).
- Manual Management: Developers must manage the memory and bounds of the array manually, increasing the risk of errors (like
ArrayIndexOutOfBoundsException).