• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code "int arr[]=new int[0]"

 
shivani vidit
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
explain that why declaring an array with above syntax nad then calling the length function on it do not gice any exception.
i mean explain the above code
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It creates an array object of size 0. It is a valid object so there would not be any exceptions thrown when you call size.

Of course, an array of size 0 isn't very useful. In fact it is pointless.

Try placing a value in index 0 and see what you get.
[ October 11, 2007: Message edited by: Robert Hill ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Hill:
Of course, an array of size 0 isn't very useful. In fact it is pointless.


Not really. There are actually a surprising number of uses for zero-length arrays. They come up quite often in Reflection, for instance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the args in a main() method started with no args and see if a zero length array is useful.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:


Not really. There are actually a surprising number of uses for zero-length arrays. They come up quite often in Reflection, for instance.


Why would it be useful in Reflection or anything for that matter?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Check the args in a main() method started with no args and see if a zero length array is useful.


It is useful to know that there was no command line arguments, but it doesn't explain why creating a zero length array in your code has any value.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can communicate the same thing to other methods that the JVM communicates to us in main(). Maybe you want an array of widgets sold in New Jersey last week, and there weren't any. Would you rather have to check the count first, handle an exception, or accept a empty array? This speaks to me pretty clearly about what will happen if there are zero or a hundred...
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are definately useful. They define a list with no items. If I have a series of steps to execute - lets just say a To-Do list - and that list is an array. What happens when I get to a day with nothing to do? Would I rather have a null value and have to check the null, adjust behavior, etc?

If there's one thing to learn, if your method returns any sort of collection - attempt to return an empty collection rather than null, unless it actually IS null.
 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do reflection mean???
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorial: The Reflection API
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An empty array can often be use as an implementation of the Null Object pattern: http://en.wikipedia.org/wiki/Null_Object_pattern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic