Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

aggregate initialization

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] array = {1,2,3} ?
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is this array of int's on the heap?
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic