Tuesday 8 November 2016

JAVA APPLICATIONS AND APPLETS AND COMMAND LINE ARGUMENTS



   Running Java Applications:
To Compile a Java program execute the compiler, javac, specifying the name of the source file on the command line as shown below:
C:\>javac Prog1.java
The javac compiler creates a file called Prog1.class that contains the bytecode version of the program.
To actually run a Java program one must use a Java application launcher, called java. For this, pass the class name Prog1 as a command line argument as shown below:
C:\>java Prog1
When Java source code is compiled, each individual class is put into its own output file named after the class and using .class extension. So it is better to give the Java source files the same name as the class they contain- the name of the source file will match the name of the .class file.

public static void main(String args[]):
o   The public keyboard is an access specifier, which allows the programmer to control the visibility of the class members. main() must be declared as public, since it must be called by code outside of its class when the program is started.
o   The keyword static allows main () to be called without having to instantiate a particular instance of the class. This is necessary since main() is called by JVM before any object are made.
o   The keyword void simply tells the compiler that main() does not return a value.
o   String args[] declares a parameter named args, which is an array of instances of the class String. Args receives any command line arguments present when the program is executed.

   Command Line Arguments:
o   Command line arguments are parameters that are supplied to the application program at the time of invoking it for execution.
o   public static void main(String args[]): Any argument provided in command line are passed to array args as its elements.
o   java CmdlineSantosh Rahul:This command line contains two command line arguments:
Santosh ->args [0]
Rahul -> args[1]
Code:
class Cmdline
{
public static void main(String args[])
{
  int n;
  n=args.length;
  System.out.println("Command Line Arguments are:");
  for(int i=0;i<n;i++)
   System.out.println(args[i]);
 }
}

No comments:
Write comments