Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many objects will be created.

 
Abdul Gafoor
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a[]=new int[10];


String ss[]=new String[10];
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many do you think will be created?
 
Abdul Gafoor
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think

this code crete only one array object
int a[]=new int[10];
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. Now what about the second one?
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
second one there are 2,
one in the stack and the other one is in pool

Dilshan Edirisuriya
SCJP
 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the second expression, it will be 22.
inside heap--> 1 String Array object,10 String Objects
inside String Literal pool--> 1 String Array object,10 String Objects.


Correct me if I'm wrong.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I considered array objects only
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another argument now I think it's 1

cos' as it is a String "array object" it doesn't create an instance in the pool.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially only the array itself will be created with null references.
The actual number of objects created by the JVM will always depend on the actual content of the arry when creating arrays of object references.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I agree
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anil Kumar Saha:
for the second expression, it will be 22.
inside heap--> 1 String Array object,10 String Objects
inside String Literal pool--> 1 String Array object,10 String Objects.


Correct me if I'm wrong.


You're wrong. The answer is 2.

This thread appears to have become more complex than is necessary to answer the original question - which has nothing to do with "the pool" (the apparently abstract concept that few understand but many like to talk about).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic