What is Array ?
Array is a fundamental data structure that is a collection of elements stored in a single variable.
Each element in an array is assigned a unique index, typically starting from zero, which allows for direct and efficient access to any element using its index.
Why should we go for Arrays?
Without arrays, we would need separate variables for each value, which is messy and inefficient.
they make it easier to store, organize, and process large amounts of data efficiently instead of handling multiple variables separately.
what is Array in java?
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Syntax:
- Declaration of an Array: int[] arr; int arr[];
- Memory Allocation (Creating an Array) After declaration, we must allocate memory using new: arr = new int[5]; // creates an array of size 5
- Declaration + Allocation in One Line: int[] arr = new int[5];
- Declaration + Initialization: If you already know the values, you can initialize directly: int[] arr = {10, 20, 30, 40, 50}; Why System.out.println(numbers); doesn’t print all values?
- In Java, numbers is an array reference variable, not the actual values.
- When you just print numbers, Java prints the memory reference (address in heap) in a default format.
That’s why you see something like:
[I@5ca881b5 //output
[ → means it’s an array
I → means it’s an array of int
@5ca881b5 → hashcode (memory reference)
Java is object-oriented at its core. An array is just another object, and printing an object without overriding toString() will never show the internal data automatically.
Top comments (0)