How is a zero-length array represented in memory?

Java primitive objects are mapped to native primitives.
So my question is how does char value[] = new char[0]; appear char value[] = new char[0]; ?
Does it depend on the implementation of the gcc compiler (on native code)? Does this mean that all empty Java String points to the same address?

+6
source share
4 answers

The memory layout is undefined because it is an implementation detail.

Here's how IBM describes the array memory layout for its 64-bit JVM:

  • 64 bits for class pointer (i.e. char signaling)
  • 64 bits for flags (e.g. saying that this object is an array)
  • 64 bit for lock data (for synchronization)
  • 64 bits for array length (only 32 bits are used, but the field is aligned on the border)
  • 0 bits for data since there are no elements in the array

A total of 256 bits or 32 bytes.

In Java, a String and a char[] are not the same thing. A String will be a separate object containing a reference to char[] .

+8
source

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

The JVM specification does not define any specific implementation for objects if they behave according to the specifications. In practice, it is implemented with a heading followed by the actual fields of the object.

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

An array allocated with zero size is an object with a title and size, but not the space allocated for real components.

An array reference is like a reference to any other object. Arrays in Java are not like arrays in C, where if the array was zero, the pointer pointing to its beginning would actually be invalid. An array reference points to an array object, which, as it turns out, has zero length and actual elements. If you try to access any element in such an array, there will be no question about valid pointers. The array reference itself points to a valid object. Then checking the boundaries will show that any index will be outside the boundaries, so further dereferencing of the pointer will not occur.

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

And this is different from null , which is a reference whose bits are zero, so it is not indicated anywhere at all. No memory is allocated except the link itself, while for char[0] , enough memory is allocated for the header and 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 will get two different empty string objects. Each of them has a separate empty array of characters that it points to. This is because the no-args constructor of the String class is implemented as follows:

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

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

Please note that if your source was:

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

Then, due to internment, they will point to the same string object and, therefore, to the same array of characters. In addition, if it was:

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

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

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

Again, a pointer to an empty string, of course, does not match a pointer to null. It points to the actual string object, which points to the actual character array object.

+7
source

Two different objects created with new must be different with respect to referential equality, so no, they are not the same object.

Separately, any two Java String references to the constant string "" will refer to the same object, because compile-time constant strings become interned.

+5
source

Because each array object has a length property when writing

 char a[] = new char[0]; 

Then, the length property gets a value of 0, which represents the size of the array. The length field is 4 bytes, and the array has a regular header, usually 8 bytes.

Nothing special in an empty array, like any other array, but it does not contain elements.

It is worth noting that an empty array and an array initialized to null are two different. For example, sometimes it’s just easier to return an empty array from a method instead of null .

+1
source

Source: https://habr.com/ru/post/986848/


All Articles