Properties of Arrays
Printing Behaviour
Character Arrays (1D char Arrays)
When a single dimensional char array is passed to print() or println(), it treats the array as if it were a string (prints concatenated characters)
Why?
As you know java is heavily inspired from C/C++ and a C-style string is nothing but an array of characters that is terminated by a special null character (\0)
Code :
char[] arr = {'H', 'i'};
System.out.println(arr);Output :
HiNOTE
Single dimensional Character array (wrapper of primitive char) is not treated as a string by Java
Code :
Character[] arr = {'H', 'i'};
System.out.println(arr);Output :
[Ljava.lang.Character;@659e0bfdNOTE
If other strings are present along with the single dimensional char array, Java no longer treats the array as a String
Code :
char[] arr = {'H', 'i'};
System.out.println("#" +arr);Output :
#[C@659e0bfdOther Arrays
Any other array (e.g., Character[], int[], String[], char[][], int[][]...) prints the class name and hash code (memory address representation) because it uses the default Object.toString() method
Code :
char[][] arr0 = {{'H', 'i'}, {'h', 'i'}};
int[] arr1 = {'H', 'i'}; // This is valid syntax since int has higher precedence over char
String[] arr2 = { "Hello" , "World" };
System.out.println(arr0);
System.out.println(arr1);
System.out.println(arr2);Output :
[[C@659e0bfd
[I@659e0bfd
[Ljava.lang.String;@659e0bfdNOTE
Multidimensional arrays of char prints class name and hashcode but if the elements of the multidimensional array are single dimensional arrays of char, java treats it as a string
Code :
char[][] arr = {{'H', 'I'}, {'h', 'i'}};
System.out.println(arr);
System.out.println(arr[0]);Output :
[[C@659e0bfd
HISome tricky code snippets
String Concatenation
Code :
String[] arr = {"Hello" + "World" , "!"};
System.out.println(arr[0]);Output :
HelloWorldHierarchy of datatypes
Code :
int[] arr = {'H' , 'i'};
System.out.println(arr[0]); // prints ASCII code of 'H'Output :
72Array Covariance & ArrayStoreException
Arrays are covariant, meaning if Integer is a subtype of Number, then Integer[] is a subtype of Number[]. This allows for polymorphic assignment but can cause runtime errors.
Read about Number Class and String as subtype of Object Class
Code :
Object[] arr0 = new String[1];
Number[] arr1 = new Integer[1];
Number[][] arr = new Number[4][];
arr[0] = new Integer[2];
arr[1] = new Long[2];
// Assignment using Anonymous Arrays
arr[2] = new Double[]{ new Double(2) };
arr[3]= new Float[]{ 1.618F }; // Golden Ratio
// The code below will compile but throws ArrayStoreException at RUNTIME
arr[0][0] = 3.14;
//The code below will raise error for Incompatible types
arr[3]= new Float[]{ 3.14 }; // There's no f or F suffix at the end so Java treats it as a double2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output :
ERROR!
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
at Main.main(Main.java:11)
ERROR!
Main.java:13: error: incompatible types: double cannot be converted to Float
arr[3] = new Float[]{ 3.14 };
^Difference between ArrayStoreException and Incompatible Types
- ArrayStoreException: A Runtime Exception (The code builds, but crashes when running)
- Incompatible Types: A Compile-Time Error (The code won't even build)
Incompatible Types (Compile-Time)
This error happens when the Compiler looks at your code and sees that two types fundamentally do not match. Since Java is statically typed, it stops you immediately
When it happens?
When you try to assign Type A to Type B, and they have no relationship (neither is a parent of the other)
Example
Integer[] arr = new String[5]; Why?
The compiler knows an Integer[] can NEVER be a String[]
ArrayStoreException (Runtime)
This error happens because of Array Covariance. The compiler lets the code pass because the assignment looks valid based on the variable types, but the actual object in memory rejects the value
When it happens?
When you use a parent reference (like Object[]) to point to a child array (Integer[]), and then try to put the wrong subclass (let's say String) into it.
Example
Object[] arr = new Integer[5];
arr[0] = "Hi"; Why?
- Compile Check: The compiler sees arr[0] = "Hi". Since arr is an array of Objects and "Hi" is also an Object(since String is a subtype of Object class). This is valid (Passes)
- Runtime Check: The JVM sees you are trying to shove a String into what is actually an Integer array in memory. It panics and throws the exception (Fails)
Summary
| Feature | Incompatible types | ArrayStoreException |
|---|---|---|
| Type | Error (Code won't run) | Exception (Code crashes) |
| Detected by | Compiler (javac) | JVM (Runtime) |
| Cause | Types have no relationship | Actual array type doesn't match the value being stored |
Arrays with final keyword
Declaring an array as final only makes the reference immutable, not the elements inside it
Code :
final int[] arr = {1, 2, 3};
int[] arr2 = {2, 3};
arr[0] = 99; // VALID: You can change the data inside
arr = new int[5]; // ERROR: You cannot point 'arr' to a new array object
arr = arr2; // ERROR: You cannot point 'arr' to an existing arrayZero-Sized Arrays
It is perfectly legal to create an array with a size of 0. This is often used when a method needs to return "no results" but wants to avoid returning null
Code :
int[] arr = new int[0];
System.out.println(arr.length); // Output: 0Jagged Arrays (Multi-dimensional)
Java doesn't strictly have multi-dimensional arrays; it has "arrays of arrays." This means sub-arrays can have different lengths
Code :
int[][] arr = new int[2][]; // Leave the second dimension empty
arr[0] = new int[3]; // First row has 3 columns
arr[1] = new int[1]; // Second row has 1 columnAnonymous Arrays
You can create an array without assigning it to a variable, often used for passing arguments to methods on the fly
Code :
// Passing an anonymous array to a method
printSum(new int[]{10, 20, 30});
// Some other uses of Anonymous Arrays
int[][] arr = new int[2][];
arr[0] = new int[]{1, 2, 3};
arr[1] = {1, 2, 3, 4}; // ERROR: This can be only be used when you are declaring the variable on the same lineSize Limitations
- Type: The size of an array must be an int. You cannot use long to define the size
Example
Code :
int[] arr = new int[1L];Output :
ERROR!
Main.java:6: error: incompatible types: possible lossy conversion from long to int
int[] arr = new int[1L];
^Example 2
Code :
int[] arr = new int['A'];
int[] arr2 = new int['A' - 64];
System.out.println(arr.length);
System.out.println(arr2.length);Output :
65
1- Max Size: Theoretically Integer.MAX_VALUE, but practically limited by available heap memory (usually slightly less than
)