• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do reflection mean???
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorial: The Reflection API
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic