This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "aggregate initialization" Watch "aggregate initialization" New topic
Author

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.)



Thanks.


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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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?

Thanks.
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: http://aspose.com/file-tools
 
subject: aggregate initialization
 
Similar Threads
Heap and Stack difference.
Best way of intialization
Study Notes from a person who secured 100%.
What is heap & stack memory?
Stack Variables and GC