• Post Reply Bookmark Topic Watch Topic
  • New Topic

Default values of args[]  RSS feed

 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the below program what are the default values passed by the JVM in order to call main() method
class program
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you type on the command line to run this program?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesha vadlamani wrote:what are the default values passed by the JVM in order to call main() method

The JVM doesn't pass any default values. If you haven't passed any parameters to your program then args will be an empty array and your code will generate an exception.
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What did you type on the command line to run this program?

compiled it normally and executes the program without giving the arguments at command prompt. But i just want to know the automatic values assigned by the jvm
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you dont give argument your program won't execute normally, it will throw arrayindexoutofbounds exception. As others have mentioned there is NO default value provided by jvm.
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful Thakare wrote:if you dont give argument your program won't execute normally, it will throw arrayindexoutofbounds exception. As others have mentioned there is NO default value provided by jvm.

thanks to all
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happened when you ran that code? Did you provide the two arguments, or did you suffer the Exception?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be clear, I would emphasize what Joanne said earlier - if you haven't passed in any parameters, you get an empty array, size 0. This is different from a null array. You could think of the empty array as a default value if you want, I suppose.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!