Java Arrays, Wat!
Java arrays are a fundamental component of the language, yet they can exhibit behaviours that surprise even seasoned developers. This article delves into some of these quirks, providing clarity and practical insights for Java developers.
Is it an Array or Not?
Consider the following declaration:
Serializable[] array2 = new Serializable[9];
Serializable array = array;
Cloneable[][] arrayA = new Serializable[9][];
Cloneable[] arrayB = arrayA;
Cloneable arrayC = arrayA;
At first glance, one might question whether array
is an array or a scalar. In reality, array
is a scalar reference that points to an array. This behaviour is consistent across Java’s type system. For instance:
Object o = new Object[9];
Here, you can assign an array to an Object
variable because arrays are also objects in Java. Additionally, arrays are Serializable
and Cloneable
, so they can be assigned to a Serializable
or Cloneable
reference as well.
classDiagram Object <|-- Array Array ..|> Cloneable Array ..|> Serializable
Where Did My []
Go?
Java syntax can produce some surprising results due to backward compatibility. Consider this method signature:
public static int method(int[]... args)[] {
return args[0];
}
In this example:
-
args
is of typeint[][]
. -
The return type is
int[]
, indicated by the[]
after the method declaration.
This syntax isn’t part of the Java Language Specification (JLS) but is permitted in OpenJDK to maintain compatibility with older codebases. Such nuances highlight the importance of understanding Java’s evolution and its impact on current coding practices.
What’s the Difference Between int[] array
and int array[]
?
The placement of brackets in array declarations can lead to subtle differences, especially when multiple variables are declared in a single statement:
int[] array, x[];
and
int array[], y[];
In these cases:
-
x
is of typeint[][]
, a two-dimensional array. -
y
is of typeint[]
, a single-dimensional array.
This distinction becomes crucial when declaring multiple arrays in one line, as the brackets associate with the variable name rather than the type. Adhering to a consistent array declaration style can prevent such confusions.
What Happens If an Array Initialisation Is Too Large?
Attempting to initialise a large array can lead to unexpected compilation errors. Consider the following initialisation:
public static final int[] VALUES = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
/* many, many lines deleted */
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
};
This code results in the following compilation error:
Error:(6, 31) java: code too large
It may seem odd that the error is not about the array size but rather the code size. This is because arrays are initialised in bytecode, which generates code to create and populate the array one element at a time. When the array is large, the generated bytecode exceeds Java’s method size limit (65,535 bytes).
Due to this limit, large initialised arrays or a high number of enum values may result in compilation errors.
Key Takeaways
-
Arrays in Java are objects and can be referenced by interfaces like
Serializable
andObject
. -
Syntax nuances, such as the placement of brackets, can affect array dimensions and types.
-
Large array initialisations may lead to compilation errors due to bytecode size limits.
-
Understanding these behaviours enhances code robustness and prevents subtle bugs.
Try It Yourself
Experiment with the provided code examples to observe these behaviours firsthand. Modify array declarations and initialisations to see how changes affect type assignments and compilation. Engaging with the code will deepen your understanding and uncover additional insights.
The Code
Code to run the examples that compile above is available here: JavaArraySurprises.java.
About the Author
As the CEO of Chronicle Software, Peter Lawrey leads the development of cutting-edge, low-latency solutions trusted by 8 out of the top 11 global investment banks. With decades of experience in the financial technology sector, he specialises in delivering ultra-efficient enabling technology that empowers businesses to handle massive volumes of data with unparalleled speed and reliability. Follow Peter on BlueSky or Mastodon.
Comments
Post a Comment