• Post Reply Bookmark Topic Watch Topic
  • New Topic

small doubt  RSS feed

 
krishnaa yadav
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we creat a 1-D array how many objects are created on heap one or two..
eg : int [] dots={6,7,8}
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the given example since it is a 1-D primitive array it will create one object on heap.

Can you elaborate your doubt so that we can explain better ?
 
krishnaa yadav
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the followin code can be written as
int[] dots;
dots = new int [3]

so if we draw a heap stack diagram it would be

2000 and 3000 assumed memory location

From line 1
------------
HEAP
2000:int[] arrobjct

STACK
2000:dots


From line 2
-------------
HEAP
2000:int[] arrobjct----->3000
3000:int[3]: 6 7 8(value would be assigned)

STACK
2000:dots

finally there are 2 objcts created one at 2000 location and other at 3000 location ....
if i am wrong please correct...

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnaa yadav wrote:the followin code can be written as
int[] dots;
dots = new int [3]


The first line only declares an array reference -- no objects are allocated (instantiated) at this time. Granted, if the variable is a local variable, then some space is allocated for it, but that doesn't make it an object.

With the second line, an object is instantiated. And the variable, declared earlier is referenced to it.

Henry
 
krishnaa yadav
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk got it

regards;
krishna
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!