Re: what is the initial value of arrays of object
George wrote:
I am using java 1.5.
class A{
A(){
}
A(int){
}
...
}
A[] a=new A[10];
For such an array, what is the initial value for each a[0]...a[9]?
null, null constructor, or something else?
They are all null. (I don't know what you mean by "null
constructor;" it's a term I've never seen before.)
Is it specified by java or
varied from compilers?
It's part of the definition of Java. Also, the compiler as
such has nothing to do with it: The array is created by the JVM
as it executes the `new' operator, and it's the JVM's business
to see to it that the array is null-filled.
Can I create the array with other constructor
A(int)?
No. A factory method could do this, but the unaided `new'
operator cannot. The closest `new' can come to something of this
kind is when you create multi-dimensional arrays:
A[][] array = new A[5][10];
After this, array[0] through array[4] are not nulls, but references
to five arrays of ten A references each. Those fifty A references
are all null, though.
Read your Java textbook, if you have one. If you don't, read
it anyway. This is elementary stuff.
--
Eric.Sosman@sun.com