While running a program at the command prompt we can pass some arguments to it. These arguments are command line arguments. They are stored in String args[ ] which we use in public static void main(String args[ ]).
e.g. c:> java Demo C++ C# C
Interpreter will take it as
c:>java Demo.main(new String[ ]={"C++", "C#", "C"})
//Program to read names from command prompt
class Demo
{
public static void main(String args[ ])
{
for(int i=0; i<args.length; i++)
System.out.println("Hello"+args[i]);
}
}
To execute this open command prompt.
c:> javac Demo.java
c:> java Demo Dipesh Shubham Chetna
Hello Dipesh
Hello Shubham
Hello Chetna
e.g. c:> java Demo C++ C# C
Interpreter will take it as
c:>java Demo.main(new String[ ]={"C++", "C#", "C"})
//Program to read names from command prompt
class Demo
{
public static void main(String args[ ])
{
for(int i=0; i<args.length; i++)
System.out.println("Hello"+args[i]);
}
}
To execute this open command prompt.
c:> javac Demo.java
c:> java Demo Dipesh Shubham Chetna
Hello Dipesh
Hello Shubham
Hello Chetna
No comments:
Post a Comment