Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what will the o/p for this ...

 
arun sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends;
Can u explain why the o/p of this program is like this:
class Test{
public static void main(String args[]){
for(int nElement = 0 ; nElement < args.length ; nElement++)
{
System.out.println("File: "+args [nElement]);
}
}
}
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can tell what do you mean by o/p?
thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you start a Java program from the command line with a few arguments then the JVM invokes the main method you show in your example, with the parameters in the args. So if you type:
java MyClass able baker charlie
then args[0] is able, args[1] is baker, args[2] is charlie. The loop in your program will print the arguments out on one line each.
Your example includes the word "File:" so I'd guess your program is expecting a list of files as arguments. If a filename includes spaces you'll have to type it in quotes so it is read as one argument and not two or three.
java MyClass "c:\program files\my file.txt"
Is that the kind of thing you were asking? Sorry if I backed up over stuff you already knew.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, arun!
Can u explain why the o/p of this program is like this:
What is the output? And do you have any ideas on why it might be that way?
can tell what do you mean by o/p?
Namaste, "o/p" likely means "output".
[ July 04, 2003: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic