Friday, June 12, 2015

Command line arguments

12:50 AM

 Command line arguments

If any input value is passed through command prompt at the time of running of program is known as command line argument by default every command line argument will be treated as string value and those are stored in string array of main() method.

Example of Command Line Argumets:

class CommandLineExample
{
public static void main(String args[])

System.out.println("Argument is: "+args[0]);

}  

compile:

Compile By > Javac  CommandLineExample.java
Run By > Java  CommandLineExample learner

output

Argument is:learner


Example:

class SumDemo
{
public static void main(String args[])

System.out.println("Sum: "+args[0]);

}

Compile By > Javac  SumDemo.java
Run By > Java  SumDemo 20 30

Output:

Sum: 50

When the above statement is executing the following sequence of steps will take place.
  • Class loader sub-system loads SumDemo along with Command line argument(10, 20) and in main memory.
  • JVM take the loaded class SumDemo along with Command line arguments (10, 20) and place the number of values in the length variable that is 2.
  • JVM looks for main() that is JVM will place the Command in the main() in the form of string class that is.

  • Hence all the CMD line arguments of java are sending to main() method available in the form of array of object of String class (every CMD are available or stored in main method in the form of array of object of String class).
  • JVM calls the main() method with respect to loaded class SumDemo that is SumDemo.main().

SOP Statements

In java language print() and println() are the predefined non-static method of printStream class used to display value or message either in the same line or line by line respectively. PrintStream class is having fixed object reference in the System class (existing as a static properties) so that either print() or println() method can be called with following syntax..

Syntax:

    System.out.print("--------------");
    System.out.println("------------");

Note:  "out" is Object reference of printStream class existing in system class as a static property.

Example:

class Hello
{
public static void main(String arg[])
{
System.out.println("Hello Learners");
}
}

output:

Hello Learners






Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top