File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String parameters at command line...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String parameters at command line......" Watch "String parameters at command line......" New topic

String parameters at command line......

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142

The 'String args[]' in the main method which we pass as argument to main at runtime, have object reference.
ie 'its initialised'!

What value is passed to it's elements? As if when we initialise any string array the default values passed are null to its elements.

for eg: String a[]=new String[2];
above statement passes 'null' to a[0] & a[1] & it displays in SOP.

But if I try to print args[0] without giving values at command line, It throws Exception ArrayIndexOutOfBoundsException.

It doesnt take null values?

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The JVM evaluates the arguments you pass. If you pass 5 arguments the JVM invokes main with

If you don't specify any arguments, the main method get's this:

which is an absolutely valid declaration for an array, even though it doesn't have any values.
Hence you ArrayIndexOutOfBoundsException.

JDBCSupport - An easy to use, light-weight JDBC framework -
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142

thanku .

I, now got it!
I agree. Here's the link:
subject: String parameters at command line......
jQuery in Action, 3rd edition