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
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.
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
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:learnerExample:
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
0 comments:
Post a Comment