Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "code "int arr[]=new int[0]"" Watch "code "int arr[]=new int[0]"" New topic
Author

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

shivani vidit
Greenhorn

Joined: Oct 11, 2007
Posts: 14
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

Joined: Feb 24, 2006
Posts: 94
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Check the args in a main() method started with no args and see if a zero length array is useful.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Oct 04, 2007
Posts: 101
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

Joined: Aug 20, 2007
Posts: 33
what do reflection mean???


Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Java Tutorial: The Reflection API


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
An empty array can often be use as an implementation of the Null Object pattern: http://en.wikipedia.org/wiki/Null_Object_pattern


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: code "int arr[]=new int[0]"