The Artima Developer Community
Sponsored Link

Design Forum
Arrays in JVM

9 replies on 1 page. Most recent reply: Jul 10, 2004 3:32 AM by Marlene Miller

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
sheetal

Posts: 3
Nickname: sheetal
Registered: Jul, 2004

Arrays in JVM Posted: Jul 4, 2004 9:27 PM
Reply to this message Reply
Advertisement
I want to know if the anewarray inst. of the jvm is supposed to develop a array of references or an array of objects? The jvm specification shows some ambiguity as far as developing an array is concerned.Can anybody plz help?


Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 7, 2004 3:47 PM
Reply to this message Reply
The Notes section of the anewarray instruction says

The anewarray instruction is used to create a single dimension of an array of object references or part of a multidimensional array.

an array of object references

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 7, 2004 4:49 PM
Reply to this message Reply
Chapter 15 Objects and Arrays of Bill Venner's Inside the Java Virtual Machine might be helpful.

http://www.artima.com/insidejvm/ed2/index.html

sheetal

Posts: 3
Nickname: sheetal
Registered: Jul, 2004

Re: Arrays in JVM Posted: Jul 8, 2004 2:59 AM
Reply to this message Reply
Thanks a lot for the reply. But still the idea of implementing the anewarray instruction of jvm is not very clear. I went to the site suggested by Marlene Miller(thanks once again). I am reproducing some part of the description over there:-

1> The anewarray instruction creates an array of object references. This instruction allocates space for the array of object references and initializes the references to null.

2> pops length, allocates a new array of objects of class indicated by indexbyte1 and indexbyte2, pushes objectref of new array.

From the 1st point I understood that an array of object references has to be created, but if the references are initialised to null where and when the references will point to objects?

From the 2nd point I understood that an array of objects has to be created.
Can anyone plz tell as to what exactly are we supposed to do b'coz the 2 statements mentioned above are quite contradicting ?

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 9, 2004 8:59 PM
Reply to this message Reply
>>From the 1st point I understood that an array of object references has to be created, but if the references are initialised to null where and when the references will point to objects?

In the expression new A[5], the elements of the array object are initialized to null. In the expression a[0] = new A(), the value of element 0 is assigned a reference to an object of type A.

----

void f() {
A[] a;
a = new A[5];
a[0] = new A();
}

void f();
Code:
0: iconst_5
1: anewarray #2; //class A
4: astore_1
5: aload_1
6: iconst_0
7: new #2; //class A
10: dup
11: invokespecial #3; //Method A."<init>":()V
14: aastore
15: return

----

I want to be sure you understand that Java arrays are different from C++ arrays.

1. A[] a;

+------+
| null | This is variable a.
+------+

Declaring a variable of array type does not create an array object or allocate any space for array components. (JLS 10.2)

2. a = new A[3];

+------------------------+
|[ null ][ null ][ null ]| This is the array object
+------------------------+

An array creation expression creates an object that is a new array whose elements are of type A. (JLS 15.10)

Array components are unnamed variables that are created and initialized to default values whenever a new object that is an array is created. (JLS 4.5.3)

3. a[0] = new A();

+-----------------------+
|[ ref ][ null ][ null ]| <----- a
+-----------------------+

+---+
| A | The element a[0] refers to this object.
+---+

An object of type A is created. The value of element 0 is a reference to the object of type A.

I want to be sure you understand that new A[3] creates an array object with components/elements/unnamed-variables initialized to the default value for the type of the component. The components must be assigned values (references to objects).

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 9, 2004 9:06 PM
Reply to this message Reply
2> pops length, allocates a new array of objects of class indicated by indexbyte1 and indexbyte2, pushes objectref of new array.

anewarray (with comments)

The count must be of type int. It is popped off the operand stack. The count represents the number of components of the array to be created.

The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2.

[*** get an index into the pool (of type names) ***]

The runtime constant pool item at that index must be a symbolic reference to a class, array, or interface type. The named class, array, or interface type is resolved (§5.4.3.1).

[*** use the index to lookup the type name ***] [*** index is a reference to a type, not a reference to an object ***]

A new array with components of that type, of length count, is allocated from the garbage-collected heap, and a reference arrayref to this new array object is pushed onto the operand stack.

[*** an array object is allocated, component objects are not allocated ***] [*** a reference to the array object is returned ***]

All components of the new array are initialized to null, the default value for reference types (§2.5.1).

[*** components are initialized to null ***]

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 9, 2004 9:09 PM
Reply to this message Reply
By the way sheetal, I have never seen the code for a virtual machine. I am putting together what I have read in the JLS, the JVMS and looked at using the disassembler javap.

- Marlene

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 9, 2004 9:24 PM
Reply to this message Reply
In C++, declaring an array variable or calling new also creates element objects.

class A {
public:
A() { cout << "A()" << endl; }
};

int main() {
A a1[3]; // create 3 A objects
A* a2 = new A[2]; // create 2 A objects
return 0;
}

A()
A()
A()
A()
A()

Not so in Java.

class A {
A() { System.out.println("A()"); }
}

class Test {

void f() {
A[] a;
a = new A[5]; // create 1 A[] object
a[0] = new A(); // create 1 A object
}

public static void main(String[] args) {
new Test().f();
}
}

A()

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 9, 2004 10:02 PM
Reply to this message Reply
I have to correct one thing above about A[] a;
The value of a is undefined (not null) if the variable is local.

Marlene Miller

Posts: 12
Nickname: marlene
Registered: Jun, 2004

Re: Arrays in JVM Posted: Jul 10, 2004 3:32 AM
Reply to this message Reply
Multi-dimensional arrays:
The values of the components of the array object might be null, or they might be references to other array objects.

A[][][] a = new A[3][][];

The first dimension of an array must be specified when the array object is created.

a[0] == a[1] == a[2] == null

Specifying more than the first dimension is shorthand for a nested set of new statements.

B[][][] b = new B[3][4][2];

is equivalent to

B[][][] b = new B[3][][];
for (int ii = 0; ii < b.length; ii++) {
   b[ii] = new B[4][];
   for (int j = 0; j < b[ii].length; j++) {
      b[ii][j] = new B[2];
   }
}

----

B[][][] b = new B[3][4][2];

b ----> [ [ref0][ref1][ref2] ]

ref0 ----> [ [ref00][ref01][ref02][ref03] ]

ref00 ----> [ [null] [null] ]

----
class A {}
class B {}
 
class Test {
   void f() {
      A[][][] a;
      B[][][] b;
 
      a = new A[3][][];
      b = new B[3][4][2];
 
      System.out.println(a[0]); // null
      System.out.println(b[2]);
      System.out.println(b[2][3]);
      System.out.println(b[2][3][1]); // null
   }
 
   void g() {
      B[][][] b = new B[3][][];
      for (int ii = 0; ii < b.length; ii++) {
         b[ii] = new B[4][];
         for (int j = 0; j < b[ii].length; j++) {
            b[ii][j] = new B[2];
         }
      }
 
      System.out.println(b[2]);
      System.out.println(b[2][3]);
      System.out.println(b[2][3][1]); // null
   }
 
   public static void main(String[] args) {
      Test t = new Test(); t.f(); t.g();
   }
}

null
[[LB;@1add2dd
[LB;@eee36c
null
[[LB;@194df86
[LB;@defa1a
null

Flat View: This topic has 9 replies on 1 page
Topic: Crystal with C# and Websphere Previous Topic   Next Topic Topic: JVM's - anewarray instruction implementation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use