• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Array declarations.  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could somebody please help me understand this.

1. What happens when we declare an array with size 0.

Following is my understanding of what happens when we type the following.

private int[] intArray1 = new int[5]; // statement 1
private SomeClass[] someClassArray1 = new SomeClass[5]; // statement 2

When statement 1 is executed an intArray1 reference is created in heap. Space is allocated in heap for an array that can hold five int values. The five int values are initialized to 0. intArray1 holds the address of this array of 5 ints.
When statement 2 is executed a someClassArray1 reference is created in heap. Space is allocated in heap for an array that can hold five object references ( At this point no space is allocated to hold a SomeClass object). The five object references are initialized to null.

Would that be correct?

So what happens when we do this.
private int[] intArray1 = new int[0]; // statement 3
private SomeClass[] someClassArray1 = new SomeClass[0]; // statement 4

Statement 3 and 4 don't give me a compilation error and statement 3 even executes without error. But what is happening behind the scenes in terms of memory allocation? Specifically I was working on an example case where a Vector object is being converted into an array using toArray( new SomeClass[0]). Why do they declare an array of size 0 while the source ( Vector in my case ) could contain many objects. And how does this toArray work then?

Please help.

Thanks,
Chan.



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:
1. What happens when we declare an array with size 0.

So what happens when we do this.
private int[] intArray1 = new int[0]; // statement 3
private SomeClass[] someClassArray1 = new SomeClass[0]; // statement 4

Statement 3 and 4 don't give me a compilation error and statement 3 even executes without error. But what is happening behind the scenes in terms of memory allocation? Specifically I was working on an example case where a Vector object is being converted into an array using toArray( new SomeClass[0]). Why do they declare an array of size 0 while the source ( Vector in my case ) could contain many objects. And how does this toArray work then?



Statement 3 creates an int array object of size zero. Statement 4 creates an SomeClass array of size zero.

As for the toArray() method, it is only using the array to get the type of the array. The overloaded toArray() method, that takes no parameters returns an Object array.

Henry
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!