Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the zero length array represented in memory?

The Java primitive objects are mapped to native primitives.
So my question is how is a char value[] = new char[0]; is being represented?
Does it depend on the gcc compiler implementation (of the native code)? Would that mean that all empty Java Strings point to the same address?

like image 566
Jim Avatar asked May 08 '15 21:05

Jim


People also ask

What is a zero length array?

Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type.

What is zero size array in C?

Zero length arrays are described as being an IBM Extension enabled by the *EXTENDED compiler option (aka -qlanglvl=extended when using ixlc). According to the compiler output *EXTENDED is in effect (which is why char d[]; is being accepted as a flexible array).

Can I declare an array with size 0?

Zero-length array declarations are not allowed, even though some compilers offer them as extensions (typically as a pre-C99 implementation of flexible array members).

How many bytes is an empty array?

The size of an empty class is not zero. It is 1 byte generally.


1 Answers

Java arrays are objects. They inherit from the Object class.

The JVM spec does not dictate any particular implementation for objects, provided they behave according to the specs. In practice, it is implemented with a header followed by the actual fields of the object.

An array in Java is not just a sequence of its primitive components. It is an object, it has the length field, and it has methods. So, like any other object, it has the header, followed by the length, followed by all the array components.

An array allocated with size zero is an object that has the header and the size but no space allocated for actual components.

A reference to an array is just like a reference to any other object. Arrays in Java are not like arrays in C, where if the array was size zero, the pointer that points to its start would actually be invalid. The reference to an array points to the array object, which happens to have length zero and no actual items. If you try to address any element in such an array, there will not be a question of valid pointers. The array reference itself points to a valid object. Then, bounds checking will show any index to be out of bounds, so no further pointer dereferencing will take place.

So the bottom line is that a reference to a char[0] is a valid reference to an actual allocated object. It simply has no data beyond the length.

And this is different than null which is a reference whose bits are all zero, thus not pointing anywhere at all. No memory other than the reference itself is allocated, whereas for char[0] enough memory is allocated for a header and a length.


As for strings, two empty strings do not necessarily point to the same character array. For example, if you write:

String a = new String();
String b = new String();

You'll get two different empty string objects. Each of them has a distinct empty character array that it points to. This is because the no-args constructor of the String class is implemented like this:

public String() {
    this.value = new char[0];
}

You see the use of the new keyword? This means a new array object is allocated, not copied from anywhere.

Note, however, that if your source was:

String a = "";
String b = "";

Then because of interning, they would be pointing to the same string object, and thus to the same character array. Also, if it was:

String a = new String();
String b = new String(a);

Then you would have two different String objects, but they would both point to the same internal character array. This is because the constructor for the second line is:

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

Again, a pointer to an empty string is certainly not the same as a null pointer. It points to an actual string object which points to an actual character array object.

like image 55
RealSkeptic Avatar answered Oct 07 '22 05:10

RealSkeptic