0

In the following example:

public class JavaExample
{
  public static void main(String[] args)
   {
   system.out.println(args[0]);
   }
}

when no parameter is passed
Exception given is java.lang.ArrayIndexOutOfBoundExpection

So my Question is that Why is that Exception is thrown , why not NullPointerException.
(I know when both expection thrown)

But in this case why the ArrayIndexOutofbound ????
Is that args is Declared and Intialized..???

Edited by ~s.o.s~: Added code tags, learn to use them.

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by de.ICeman
1

args is declared in your code, but it is inialised at run time with copies of the arguments that were entered on the command line. If there were no arguments it's initialised to an array of length zero (not null, just an empty array). So an attempt to reference the first element of the array [0] is an index out of bounds error.

0

Hmmm i didn't realize that till u told ....... Thanks from my heart.....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.