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.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 5
    Almost correct. However, the instance of C is not part of the array (counter to what your red boundary implies). – Oliver Charlesworth Sep 26 '14 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. – Bill Rosmus Sep 26 '14 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. – David Ehrmann Sep 26 '14 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? – overexchange Sep 26 '14 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. – Oliver Charlesworth Sep 26 '14 at 23:36
  • @DavidEhrmann *"and it might not be contiguous"* - Do you mean that the references of the elements in the array are not stored contiguously in memory (IMO they are, but I'd like to be corrected if I'm wrong)? Or were you referring to the fact that the elements of the array are objects from the heap that could be anywhere? – Joffrey Sep 26 '14 at 23:39
  • @BillR Hope i addressed your point by modifying the query. – overexchange Sep 26 '14 at 23:40
  • http://stackoverflow.com/questions/20646022/how-does-arrays-work-in-the-bytecode-of-java nitty gritty detail in the answer here – Lorenzo Boccaccia Sep 26 '14 at 23:43
  • @OliverCharlesworth you mean object1 `[1234, null]` is of type `class [LC` and object2(instance of C) is of type `class C` and both are different objects. Both objects sit in heap. reference variable c[0] of object1 points to object2 with reference at address(say 1234). Is that correct? – overexchange Sep 26 '14 at 23:49
  • "The instance of C is not part of the array": one way to understand this is that it's possible for the array to get garbage-collected while the instance may still have references pointing to it so it can't be garbage-collected. – ajb Sep 27 '14 at 00:00
  • @ajb Ok. But when we say: 'they're separate objects', Does that mean, object1 `[1234, null]` is of type `class [LC` and object2 is of type `class C` . Both objects sit in heap. reference variable `c[0]`( of object1) points to object2 with referring to address(say 1234). Is that correct? – overexchange Sep 27 '14 at 00:06
  • @overexchange I think that's generally correct. – ajb Sep 27 '14 at 00:09
  • By not contiguous, he means that the different instances of objects C that will be created (for example c[0] and c[1]) might not be stored contiguously in the memory. Only the array of pointers c[] itself is. Also, in Java, all pointers are double pointers in order to allow for the compaction of memory when the garbage collection is done (for most but not all versions of Java). Therefore, c doesn't point directly toward the array c[] but first to another pointer and this pointer toward c[]. Same thing for all pointers of c[] toward the objects C. This is why Java is slower than C++. – SylvainL Sep 27 '14 at 00:10
  • @SylvainL Do you think the second diagram in the query will make understand the idea of 'contiguous'? Here instance of C is created in some random space. – overexchange Sep 27 '14 at 00:33
  • @SylvainL "This is why Java is slower than C++" http://stackoverflow.com/questions/145110/c-performance-vs-java-c – m0skit0 Sep 27 '14 at 00:39
  • A lot of people have worked on both C/C++ on one side and C#/Java on the other side and I don't remember anyone of those telling that C#/Java was faster or even just to be of comparable speed to C/C++. In fact, many of us have begun coding with C/C++ before starting with Java and the first comment usually was often "How sluggish it is to work with Java!". There is a reason why there are a lot of people willing to mix C++ with Java for those "need to be really fast" routines. – SylvainL Sep 27 '14 at 01:32
  • On the other hand, given a good JIT compiler and time for it to settle in and optimize the code, Java isn't necessarily significantly slower than most compiled languages. If it was, you wouldn't have companies like IBM using Java to write major applications. – keshlam Sep 27 '14 at 01:43
  • Speed is only one of many factors to consider when choosing a language or a mix of languages to perform a task. – SylvainL Sep 27 '14 at 01:56
  • @SylvainL I did not get your point: `Also, in Java, all pointers are double pointers in order to allow for the compaction of memory when the garbage collection is done (for most but not all versions of Java).` – overexchange Sep 28 '14 at 13:17
  • First, technically, in Java, you don't have pointers; only references but a lot of people still call them pointers by habit. These references are represented internally by double pointers (a pointer to a pointer); which give the runtime the possibility of compacting most of the memory when the garbage collection is done. However, it's not all the versions of Java who perform this. For example, with Java for the first versions of Android, there is no compaction of memory (but I'm not sure if this is still the case for the latest versions as I didn't check). – SylvainL Sep 28 '14 at 18:02
  • @OliverCharlesworth In the above query, if i say `class C implements I{}` where `interface I{}` is an interface, then Does `class [LC` becomes a subclass of `class [LI`? i know that `class [LI` is subclass of `class Object`. – overexchange Sep 29 '14 at 01:44
  • @SylvainL i did not get your answer about double pointers. – overexchange Sep 29 '14 at 01:46
  • You should read about the garbage collection process of Java; this is explained in full details. These little comments are definitely not the place to try explaining these concepts. – SylvainL Sep 29 '14 at 02:05

1 Answers1

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.

SylvainL
  • 3,926
  • 3
  • 20
  • 24
  • Does `class [LC` implement any interface?if yes, let me know the name or definition of that interface – overexchange Sep 28 '14 at 04:41
  • Not to my knowledge. You cannot create an object that will emulate an array natively. – SylvainL Sep 28 '14 at 07:47
  • 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? – overexchange Sep 28 '14 at 13:20
  • 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. – SylvainL Sep 28 '14 at 17:33
  • 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 l*ike a normal class*, either in Java source code, bytecodes, or reflectively. – Stephen C Apr 07 '17 at 15:08