Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic