• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 9689
42
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic