File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes aggregate initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "aggregate initialization" Watch "aggregate initialization" New topic

aggregate initialization

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
What is aggregate initialization of array? I have tried to search the web for this but could not really figure out what this term refers to.

All objects of non-primitive types can be created only via new. There�s no equivalent to creating non-primitive objects "on the stack" as in C++. All primitive types can be created only on the stack, without new. There are wrapper classes for all primitive classes so that you can create equivalent heap-based objects via new. (Arrays of primitives are a special case: they can be allocated via aggregate initialization as in C++, or by using new.)


We must know, we will know. -- David Hilbert
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

int[] array = {1,2,3} ?

[My Blog]
All roads lead to JavaRanch
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
I don't know. I guessed that's what it means.
Is array on the heap?
Are those three int's on the stack now?

How do you create this array by new?

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It's just a convenient short-hand. Nothing magic happens. It's basically the same as new-ing an array, then filling in the entries.

Maybe the "aggregate initialisation" version generates more efficient byte-code; I dunno and won't be trying it myself. Anyone want to compare the byte-code?

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
So is this array of int's on the heap?
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Yes, it's an array on the heap

Declares an int array reference variable with name array
Creates an int array with a length of three elements
populates the array's elements with value 1,2,3
Assigns the array object to the variable array

The array object will be created on the heap.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you.

Are reference variables located on the stack? I mean the reference itself (~the pointer), not the address where it points to, which is in the heap.
I agree. Here's the link:
subject: aggregate initialization
It's not a secret anymore!