Home TechnologyProgrammingJava Subscribe to RSS

Why Do We Use (String[] Arg) In Main Function In Java?

Pl i need the answer because i searched in many websites but i didn't get clear explanation.....

Answer Question

3 Answers - Sort by: Date | Rating

    This is where java sticks the array of arguments from the command line (if there is one)...  

    Example...  If you have a program and to invoke it you call the following:

    Java helloWorld -s -h -f:c:\test.txt

    Arg would contain the following values:
    Arg[0] = "-s"
    Arg[1] = "-h"
    Arg[2] = "-f:c:\test.txt"

    This is primarily used for command-line applications.
    0 0

    Tazbill  

    answered 2 years ago

      Because it helps in accepting values for variables unless you want to use data input output functions(which is not that simple).
      0 0

      Fervid  

      answered 2 years ago

        If you run java from command line then you pass in arguments with the program that you can use later on in the code. Arguments in this case are just values that you want passed into the code.

        Example:
        Student.java John Taylor
        //in this case arg[0] = John arg[1] = Taylor

        public static void main(String[] args){
                  //the following will print to the screen John Taylor
                  System.out.println(arg[0] + " " + arg[1]);
        }
        0 0

        Gujunilesh  

        answered 6 days ago

        More

           
           

          Ask a Question via Twitter

          Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

          Blurtit Store

          Get T-shirts, hoodies, caps and more at the Blurtit store

          Blurtit International