• Post Reply Bookmark Topic Watch Topic
  • New Topic

String array of main  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I hope this question is not dumb...

If I do not provide any arguments on the command line then will the string array of main be empty or null?
I tried the following code to find out.... but it shows an "ArrayOutOfBoundsException" error...Why is that ?

public class trial
{

public static void main(String args[])
{
System.out.println(args[0]);

}


}


 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't pass in any arguments to the main method, the array is empty (length=0) not null. This is why when you try args[0] gives you ArrayIndexOutBoundsException rather than NullPointerException or anything else.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No arguments = array length 0 so IndexOutOfBoundException. Even If you provide only one aggument and try to access access second one by writting code array[1], you would get same exception.

[It better to ask a silly question than making a silly mistake ]
 
rachel biji
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I understand ...thanks a lot!!!....

But if an array is empty and not null.....doesnt that nean that storage space is created for it(since the array exists)???
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating an array is like getting out a new piece of paper and writing "My List of Stuff" across the top and writing n numbers down the side.

A zero length array has no numbers down the side, so if someone asks for the first item on the "My List of Stuff" list, you must reply: "ummm, [shrug], there's isn't one".

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!