Array variable assignment
Case 1:
import java.lang.*;
class Test {
public static void main(String[] args) {
int[] x = {10,20,30,40};
char[] ch = {'a','b','c','d'};
int[] b = x; // VALID
int[] c = ch; // INVALID: Compile time error: Incompatible types, found char[], required int[]
}
}
Element level type promotions are not applicable at array level. For example, char
element can be promoted to int
type, whereas char[]
cannot be promoted to int[]
. This is because char[]
is a object of type [C
and int[]
is a object of type [I
, and there is no relation between the two classes.
import java.lang.*;
class Test {
public static void main(String[] args) {
String[] s = {"A", "B", "C"};
Object[] a = s; // VALID
}
}
The only exception to this is when promoting to an Object[]
. In the case of Object[]
, child class array can be promoted to parent class type aray.
Case 2:
import java.lang.*;
class Test {
public static void main(String[] args) {
int[] a = {10,20,30,40,50,60};
int[] b = {70,80};
a = b;
}
}
When assigning one array to another array, internally, elements won't be copied, just the reference variables will be re-assgined. Hence it does not matter if the arrays are different sizes as the number of elements are not re-assigned.
import java.lang.*;
class Test {
public static void main(String[] args) {
int[] a = new int[3][];
a[0] = new int[4][3]; // INVALID: Compile time Error: Incompatible types: Found: int[][], Required: int[]
a[0] = 10; // INVALID: Compile time Error: Incompatible types: Found: int, Required: int[]
a[0] = new int[2]; // VALID
}
}
Whenever assigning one array to another array, both dimensions and data-type should be matched. E.g. in place of 1 dimensional int array, we should only provide a 1 dimensional array. If any other dimension is provided, then compile-time error will be thrown.
import java.lang.*;
class Test {
public static void main(String[] args) {
for(int i = 0; i <= args.length; i++) {
System.out.println(args[i]);
}
}
}
java Test.java a b c
a
b
c
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Test.main(Test.java:6)
java Test.java a b
a
b
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at Test.main(Test.java:6)
java Test.java
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at Test.main(Test.java:6)
import java.lang.*;
class Test {
public static void main(String[] args) {
String[] argh = {"x","y","z"};
args = argh;
for(String s : args) {
System.out.println(s);
}
}
}
java Test.java a b c
x
y
z
java Test.java a b
x
y
z
java Test.java
x
y
z
import java.lang.*;
class Test {
public static void main(String[] args) {
int[][] a = new int[4][3]; // 5 objects created: base array 'a' plus 4 arrays of size 3
a[0] = new int[4]; // 1 object created 'a[0]'
a[1] = new int[2]; // 1 object created 'a[1]'
a = new int[3][2]; // 4 objects created: base array 'a' plus 3 arrays of size 2: all 7 objects created before are garbage collected
}
}