Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

String array of main

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic