This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array of 0 elements .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array of 0 elements .." Watch "Array of 0 elements .." New topic
Author

Array of 0 elements ..

Tejas Tambe
Ranch Hand

Joined: Oct 08, 2001
Posts: 47
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

Joined: Oct 11, 2001
Posts: 204
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


nss
Tejas Tambe
Ranch Hand

Joined: Oct 08, 2001
Posts: 47
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

Joined: Apr 20, 2000
Posts: 3252
Now what if someone calls Application.getActiveUsers() while there are no such users at that moment?
- Peter
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array of 0 elements ..