This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of 0 elements ..

 
Tejas Tambe
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khalid Mughal says, in Java arrays of 0 elements can be CONSTRUCTED.
Suppose i declare an array as :
int[] arr1 = new arr[0];
This line of code compiles fine. But when executed it fires a ArrayOutOfBoundException. In such a case a question just popped up, why then allow to create an array of 0 elements. What is achieved ??
Please help me to understand this funda better,
Thanx in advance,
Tejas
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tejas,
You can create an array of length 0
if you type arr1.length, output will show you that it is 0 length.
but if you try to type arr1[0] then it will definetely throw runtime excep. ArrayIndexOutOfBoundException as u are asking for the first element arr1[0] which it does not have since it is of length 0
hope it helps
Regards
Neha
 
Tejas Tambe
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx neha,
but the thing that's bugging me is that WHY ALLOW TO CREATE array of size 0. What is achieved ???
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now what if someone calls Application.getActiveUsers() while there are no such users at that moment?
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic