5

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.

enter image description here

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.

enter image description here

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.

23
  • 5
    Almost correct. However, the instance of C is not part of the array (counter to what your red boundary implies). Commented Sep 26, 2014 at 23:30
  • What are you hoping to learn from this? Why do you want to know. That can help someone provide a better answer for you. Commented Sep 26, 2014 at 23:31
  • There's also the details that the memory for the array will hold the array's size, type, and it might not be contiguous. Commented Sep 26, 2014 at 23:33
  • @OliverCharlesworth I did not get you when you say, instance of C is not part of the array. Instance of C sits in heap, unlike reference variables, correct? Commented Sep 26, 2014 at 23:35
  • 1
    Both the array and the C instance are objects, so they're both on the heap (at least, conceptually). But they're separate objects. Commented Sep 26, 2014 at 23:36

1 Answer 1

2

To answer your question: yes, in Java (and C#), nearly everything is split into multiple discrete chunks of memory accessed by pointers. This not only include your bidimensional array but also any embedded object inside your object C. In C++, if you have an array (single dimensional or not) of 10 objects and each of these object contains 10 embedded objects, you can allocate all this with a single piece of memory. In C# and Java, you will have a minimum of 101 memory allocations for storing all this (if all of the embedded objects are simple objects) in the case of a mono-dimensional array and more in the case of a multi-dimensional array.

However, this explosion of pieces of memory shouldn't be seen a something very bad because it's free you of the difficulty of managing yourself the allocation of memory as you can have with C/C++ and in most cases, the power of any modern CPU is generally sufficient to pull it forward at a sufficient speed.

Sign up to request clarification or add additional context in comments.

5 Comments

Does class [LC implement any interface?if yes, let me know the name or definition of that interface
Not to my knowledge. You cannot create an object that will emulate an array natively.
When we say that reference variable c points to an instance of class [LC after executing C[] c = new C[2]. Does the constructor of class [LC get called to instantiate?
Internally, the memory for the object c is initialised to the correct values by the runtime. However, you don't have access to this behavior: you cannot call it directly or change it from your Java coding. Therefore, weither you call this initialisation a constructor or whatever else, it's up to you. For what I remember, C++ give you access to some part of this process but Java not.
The class [LC; is a subclass of Object, and inherits all of the methods in the Object API. However, obviously, Object is a class not an interface. However, the class [LC; exists in a conceptual sense only. It has no code, no methods of its own and cannot be instantiated like a normal class, either in Java source code, bytecodes, or reflectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.