• Post Reply Bookmark Topic Watch Topic
  • New Topic

why we are using 'new' to declare primitive array  RSS feed

 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

int,float,short these are all primitive datatypes not classes,
but for array declaration of these primitives we r using 'new' though these are not classes
int a[] = new int[4];

what's happening exactly here
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yers the items in the array are primitives and do not need a "new". The
array, however, is not a primitive. An array is an Object, and that is
what is being initialized with the "new" statement.
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for u r reply
can u please expalin me what's happening exactly when we say 'new' on primitive .

what i mean is when u say 'new' for a class it internally executes constructor of that class.

what's happening in the case of primitive arrays.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at it a little more deeply, not only does new call the constructor, it also allocates memory for the instance variables. I think this is more important to understand since it is the same with using new to create an array: you are allocating memory for the array.

Layne
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can u please expalin me what's happening exactly when we say 'new' on primitive .


you cannot say new on a primitive.
if you say new int[4], you say new on an array, not on the int primitive...

:-)

jan
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!