This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array of 0 elements .." Watch "Array of 0 elements .." New topic

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,
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

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
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
I agree. Here's the link:
subject: Array of 0 elements ..
It's not a secret anymore!