This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String parameters at command line......" Watch "String parameters at command line......" New topic
Author

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?


thanks
Jyo
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!
 
 
subject: String parameters at command line......
 
Similar Threads
Question about regular expressions
NullPointerException
ArrayIndexOutOfBounds exception
using getopt() in Java
Command line arguments help.