• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object creation on Array Initialisation

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the code above creates 3 objects on heap, one array and two thread objects. and the array will hold reference to these object in corresponding index position.
That's fine.


here only one object is created on heap. what about the primitive values that are being assigned to array, where are they stored???

is it the bit pattern of those values that is stored in the corresponding index position's allocated memory.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here only one object is created on heap. what about the primitive values that are being assigned to array, where are they stored???


The values are stored in the array itself. Each element in the array will store the int value. When you create an array of references (like your first example), then each element in the array stores the reference variable (and the reference variables points to an objects on the heap), in case of a primitive value, the actual value is stored in the array...
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

here only one object is created on heap. what about the primitive values that are being assigned to array, where are they stored???


The values are stored in the array itself. Each element in the array will store the int value. When you create an array of references (like your first example), then each element in the array stores the reference variable (and the reference variables points to an objects on the heap), in case of a primitive value, the actual value is stored in the array...



Thanks for the explanation
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic