• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String parameters at command line......

 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanku .

I, now got it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic