File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How do arrays get created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How do arrays get created" Watch "How do arrays get created" New topic
Author

How do arrays get created

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
My question is, how do arrays really get created. For instance when you say,

A a=new A();

it calls class A's constructor, which calls Object class's constructor creating the object. But how does it work with arrays. What really happens when you say

int[]x=new int[2];

???

Thanks in advance
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

Oceana Wickramasinghe wrote:For instance when you say,

A a=new A();

it calls class A's constructor, which calls Object class's constructor creating the object.

That's not actually true (and this might clear up the rest of the question). Calling a constructor doesn't create the object. The JVM creates the object, and then calls the constructor to carry out any initialisation that's necessary.

With an array, no such initialisation is necessary. The virtual machine just creates the array with nulls/zeros in every slot.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Matthew Brown wrote:
Oceana Wickramasinghe wrote:For instance when you say,

A a=new A();

it calls class A's constructor, which calls Object class's constructor creating the object.

That's not actually true (and this might clear up the rest of the question). Calling a constructor doesn't create the object. The JVM creates the object, and then calls the constructor to carry out any initialisation that's necessary.

With an array, no such initialisation is necessary. The virtual machine just creates the array with nulls/zeros in every slot.


Thanks, so that means that when you create an array no constructors get called?
kammcs singh
Greenhorn

Joined: Aug 22, 2011
Posts: 1
Declaring and constructing array means creating the array object on the heap i.e doing a new on the array type .To create an array object we just provided its size and
JVM do the things to allocate the space on heap memory
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
kammcs singh wrote:Declaring and constructing array means creating the array object on the heap i.e doing a new on the array type .To create an array object we just provided its size and
JVM do the things to allocate the space on heap memory


Thanks but my question is, when you say new int[], do any constructors get called. When you create a normal object you say new Object(), but here you use square brackets, which makes me wonder if no constructors get called. And another question, when i say new A(), that object belongs to class A. When i say new int[] which class does that array object belong to? I just want someone to clarify the technical aspect of this process.
Atul Shrivastava
Greenhorn

Joined: Jan 12, 2009
Posts: 19

I think arrays are not represented by a CLASS. So, there could not be a concept of calling constructors on them...

When you say int[]x={1,2,3};, an array is created on the heap, and the starting address of that is assigned to the 'reference' variable x.

Hope it clarifies.


OCP (SCJP) - Java 1.6
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Oceana Wickramasinghe wrote:

Thanks but my question is, when you say new int[], do any constructors get called. When you create a normal object you say new Object(), but here you use square brackets, which makes me wonder if no constructors get called. And another question, when i say new A(), that object belongs to class A. When i say new int[] which class does that array object belong to? I just want someone to clarify the technical aspect of this process.


First : int is a primitive type , when you say 5 times the 4 bytes gets allocated in sequential,4 blocks.
Second: If Person is a class and when you say it won't call Constructor a default of null would be initialized and for int 0 is initialized.hope this helps!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do arrays get created