This query is posted to basically understand points like
An object is class instance or an array;
An array is a subclass of
Object
class;Everything that is instantiated other than primitive is an object in Java.
Here is my understanding of working with arrays in Java.
Considering the below program,
/* dummy.java */
class C {
private int i;
public C() {
i = 1;
System.out.println("Am in constructor");
}
}
public class dummy {
public static void main(String[] args) {
C[] c = new C[2]; // Line 11
c[0] = new C();
System.out.println(c);
}
}
An object of type class [LC
is created in run-time after running,
C[] c = new C[2]; //Line 11
In the above code. class [LC
is an immediate subclass of Object
class. Reference variable c
points to this object (shown in red boundary below) after running Line 12
in the above code. Reference variables sit in stack and an object of type class C
will go in heap.
For a below change of line 11 & 12
in the above code
C[][] c = new C[2][2];
c[0][0] = new C();
will make the representation as shown below.
Is my understanding correct? If yes, Can you please explain more on usage of class [LC
in run time to instantiate an object?
Note: C[].class
gives the actual type in run-time, which is class [LC
.