25 Interesting Facts about Arrays in Java
Last Updated :
04 Dec, 2023
Java arrays are the workhorses of many programs, offering a structured way to store and manipulate data. While arrays are a fundamental concept, there are intriguing facts that developers may not be aware of for Java Arrays. In this blog post, we'll explore 25 interesting facts about arrays in Java, shedding light on their nuances and capabilities.
1. Zero-Based Indexing:
Arrays in Java use zero-based indexing, meaning the first element is accessed with index 0, the second with index 1, and so on.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] numbers = { 10, 20, 30 };
int firstElement
= numbers[0]; // Accessing the first element
// (index 0)
System.out.println(firstElement); // Output: 10
}
}
2. Fixed Size:
The size of an array in Java is fixed upon creation and cannot be changed afterward
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] fixedArray
= new int[5]; // Array with a fixed size of 5
}
}
3. Primitive and Object Types:
Arrays in Java can store both primitive data types (int, char, etc.) and objects.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] primitiveArray = { 1, 2, 3 };
String[] objectArray
= { "Java", "Arrays", "Example" };
}
}
4. Reference Variables:
Arrays are objects in Java, and when declared, a reference variable is created.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] arrayReference = new int[3];
}
}
5. Dynamic Initialization:
While the size of an array is fixed, its elements can be dynamically initialized.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] dynamicArray = new int[3];
dynamicArray[0] = 10;
dynamicArray[1] = 20;
dynamicArray[2] = 30;
}
}
6. Array Copying:
Java provides methods like System.arraycopy()
and Arrays.copyOf()
for efficient copying of arrays.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] sourceArray = { 1, 2, 3 };
int[] destinationArray = new int[3];
System.arraycopy(sourceArray, 0, destinationArray,
0, sourceArray.length);
}
}
7. Varargs and Arrays:
Varargs (variable-length argument lists) in Java are implemented using arrays.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
void printNumbers(int... numbers)
{
for (int num : numbers) {
System.out.print(num + " ");
}
}
printNumbers(1, 2, 3, 4); // Output: 1 2 3 4
}
}
8. Arrays.asList():
The Arrays.asList()
method facilitates the conversion of arrays to Lists.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
String[] languages = { "Java", "Python", "C++" };
List<String> languageList
= Arrays.asList(languages);
}
}
9. Array Equality:
Arrays do not override the equals()
method in Java's Object class.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] array1 = { 1, 2, 3 };
int[] array2 = { 1, 2, 3 };
boolean areEqual = Arrays.equals(array1, array2);
System.out.println(areEqual); // Output: true
}
}
10. The Enhanced for Loop:
The enhanced for loop introduced in Java 5 simplifies array traversal.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] numbers = { 1, 2, 3 };
for (int num : numbers) {
System.out.print(num + " ");
}
// Output: 1 2 3
}
}
11. Cloneable Interface:
Arrays implement the Cloneable
interface, allowing developers to create a copy of an array using the clone()
method.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] originalArray = { 1, 2, 3 };
int[] clonedArray = originalArray.clone();
}
}
12. Arrays.deepToString():
For multi-dimensional arrays, the Arrays.deepToString()
method provides a convenient way to print the contents.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[][] matrix
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
System.out.println(Arrays.deepToString(matrix));
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
}
}
13. Arrays.sort():
The Arrays.sort()
method efficiently sorts arrays.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] unsortedArray = { 3, 1, 4, 1, 5, 9, 2 };
Arrays.sort(unsortedArray);
System.out.println(Arrays.toString(unsortedArray));
// Output: [1, 1, 2, 3, 4, 5, 9]
}
}
14. Parallel Array Sorting:
Starting from Java 8, arrays can be sorted in parallel using Arrays.parallelSort()
.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] unsortedArray = { 3, 1, 4, 1, 5, 9, 2 };
Arrays.parallelSort(unsortedArray);
System.out.println(Arrays.toString(unsortedArray));
// Output: [1, 1, 2, 3, 4, 5, 9]
}
}
15. Jagged Arrays:
Java supports jagged arrays, where each row in a multi-dimensional array can have a different length.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[][] jaggedArray
= { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };
}
}
16. Memory Efficiency:
Arrays in Java are memory-efficient, storing elements of the same type contiguously in memory.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] initializedArray = { 1, 2, 3, 4, 5 };
}
}
17. Array Initialization Shortcut:
When declaring an array, Java allows direct initialization with values.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] initializedArray = { 1, 2, 3, 4, 5 };
}
}
18. Array Length Attribute:
The length
attribute of an array provides a quick way to obtain its size.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] array = { 10, 20, 30 };
int arrayLength = array.length;
}
}
19. ArrayDeque vs. Arrays:
While ArrayDeque
is a dynamic array-based implementation of a deque, arrays themselves are a fundamental building block for such data structures.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> deque = new ArrayDeque<>();
deque.add(1);
deque.add(2);
deque.add(3);
}
}
20. Arrays.fill():
The Arrays.fill()
method efficiently populates an entire array with a specified value.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] filledArray = new int[5];
Arrays.fill(filledArray, 42);
}
}
21. Garbage Collection:
Arrays, like other objects in Java, are subject to garbage collection.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] arrayToBeCollected = { 1, 2, 3 };
arrayToBeCollected
= null; // Making the array eligible for garbage
// collection
}
}
22. Array Reflection:
Java's reflection API allows developers to inspect and manipulate arrays at runtime.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
Class<?> arrayClass = Class.forName(
"[I"); // [I represents an int array
}
}
23. Arrays and Generics:
Arrays and generics have some compatibility issues due to type erasure.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Generic array creation is not allowed
List<String>[] arrayOfLists = new List[5];
}
}
Accessing elements in an array is a constant-time operation, making arrays highly efficient for random access scenarios.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] array = { 1, 2, 3, 4, 5 };
int thirdElement = array[2];
}
}
25. Arrays in Streams:
With the introduction of the Stream API in Java 8, arrays seamlessly integrate into stream processing.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = Arrays.stream(numbers).sum();
System.out.println(sum); // Output: 15
}
}
Understanding these facts about arrays provides a solid foundation for leveraging their power in Java programming. Arrays in Java are a foundational element of the language, offering a powerful mechanism for data storage and manipulation. Understanding the intricacies of arrays enhances a developer's ability to write efficient and error-free code. Whether dealing with simple arrays or complex multi-dimensional structures, these 25 facts illuminate the diverse capabilities and nuances of arrays in Java.
Similar Reads
Interesting facts about Array assignment in Java
Prerequisite : Arrays in Java While working with arrays we have to do 3 tasks namely declaration, creation, initialization or Assignment. Declaration of array : int[] arr; Creation of array : // Here we create an array of size 3 int[] arr = new int[3]; Initialization of array : arr[0] = 1; arr[1] =
4 min read
Interesting Facts About Java
Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was c
2 min read
Initialize an ArrayList in Java
ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.ArrayList inherits the AbstractList class and imp
4 min read
Final Arrays in Java
In Java, final is a keyword that is used to make a variable constant. Once we declare a variable as final, its value can not be changed throughout the program. But when we use final with an array, it is a bit different thing. Final applies to the reference, not the contents. This means we cannot rea
4 min read
Inserting Elements in an Array - Array Operations
In this post, we will look into insertion operation in an Array, i.e., how to insert into an Array, such as:Insert Element at the Beginning of an ArrayInsert Element at a given position in an ArrayInsert Element at the End of an ArrayInsert Element at the Beginning of an ArrayInserting an element at
2 min read
How to Initialize an Array in Java?
An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java
5 min read
Arrays Class in Java
The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of an Object class. The methods of this class can be used by the class name itself.The
15 min read
Difference between Arrays and Collection in Java
An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the co
3 min read
Internal Working of ArrayList in Java
ArrayList is a resizable array implementation in Java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object classes. ArrayList class in Java has 3 constructors. It has its own version of readObject and wri
10 min read
Character Array in Java
In Java, a character array is a data structure used to store a sequence of characters. The characters are stored in contiguous memory locations and can be accessed by their index, similar to an array of integers or any other data type. Declaring a Character Array A character array can be declared in
5 min read