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..???

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.