- Array is a collection of similar data types stored in under a common name.
*Its stored homogeneous data into array.
*It helps you store multiple values in a single variable instead of declaring separate variables for each value.
*Array value store in heap memory
*Here JVM will manage to create the memory for the Array object.
*It is a index based.
*The starting index will be 0.
*Each element is accessed by its index, starting from 0.
*In Java, the size of an array cannot change once it is created.
*You can modify the elements inside it, but not the length.
*No import is needed for arrays in Java.Arrays are built into the Java language itself — they are part of the core language.
If you want to use these methods Arrays.sort(), Arrays.toString() should import this package **import java.util.Arrays;*.
- — but it can store both primitive and non-primitive values inside it.
WHY WE USE ARRAY:
*We use arrays in Java because they let us store and manage multiple values efficiently using a single variable name — instead of creating many separate variables.
*Store multiple values together
Instead of many variables, one array holds many items.
*Easy looping
We can access all elements using a loop.
*Fast access by index
Access any element directly using its index (like marks[2]).
*Memory efficiency
Elements are stored contiguously in memory.
ARRAY SYNTAX:
Declaration and then creation
dataType[] arrayName = new dataType[size];
Example:
int[] numbers = {10, 20, 30, 40, 50};
Declare the size of the Array:
int[] numbers; // Declaration
numbers = new int[3]; // Creation
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
Getting array size:
System.out.println(numbers.length); // prints number of elements
Advantages of arrays:
*Stores multiple values in a single variable.
*Easy access using index.
*Useful for calculations and data analysis.
*Memory efficiency.
Simplifies code.
*
Disadvantages of arrays:**
*Fixed size.
*Once an array is created, its size cannot be changed.
*Can store only one data type.
*3️⃣ No built-in methods for easy operations
Arrays do not have built-in methods for:
Adding or removing elements
Searching specific items
Checking if an element exists.
*Wastage or shortage of memory
Because size is fixed:
If you allocate too large, memory is wasted.
If you allocate too small, it can’t hold all data.
Top comments (0)