• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt about array construction  RSS feed

 
anshuman roy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i have a doubt if any could clarify them??
while construction of an array we write
int[] houses=new int[20];
and thus an array object having reference of "houses" is constructed.
now my doubt is that how is this object being constructed without using the constructor.

thanks,

ansuman
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is a unique object type in Java.
 
anshuman roy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers can u clarify a bit more on this . what is unique objects??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn means that Arrays are special cases in java - they work a little differently than other classes.

so when you say

int[] houses=new int[20];

Java knows you are making an array of ints (or whatever you're making an array of, and knows how to make that array.

i THINK it is just grabbing a contiguous block of memory large enough to hold your 20 int references, but i wouldn't swear to that. It then knows how to get the correct one based on it's offset.
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java handles arrays separately,

If you create int array it initializes every element to zero.
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java whenever you create arrays it initializes with the default values of that java type. This is the behavior of java arrays.
 
anshuman roy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers guys!!
but still i could not understand what is the role of new keyword over here. As i know new is followed by only a constructor. but here there is now constructor.

cheers.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new IS followed by a constructor. it's being followed by an ARRAY constructor, which is a special case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!