Forums Register Login

aggregate initialization

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
int[] array = {1,2,3} ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
So is this array of int's on the heap?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Bras cause cancer. And tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2836 times.
Similar Threads
What is heap & stack memory?
Study Notes from a person who secured 100%.
Heap and Stack difference.
Best way of intialization
Stack Variables and GC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:16:07.