user3921
user3921

Reputation: 241

Basic beginners in Java: what does 'arguments' mean in Java

iv'e been trying to learn basic java programming for the last 2 days, and I encountered an issue i can't figgure while viewing the following code:

class DayCounter {
    public static void main(String[] arguments) {

        int yearIn = 2008;
        int monthIn = 1;
        if (arguments.length > 0)
            monthIn = Integer.parseInt(arguments[0]);
        if (arguments.length > 1)
            yearIn = Integer.parseInt(arguments[1]);
        System.out.println(monthIn + "/" + yearIn + " has "
            + countDays(monthIn, yearIn) + " days.");
    }
}

I can't understand the line if (arguments.length > 0) what does arguments mean? Where did the value come from?

Upvotes: 6

Views: 26121

Answers (8)

John Doe
John Doe

Reputation: 2924

First Understand the meaning of code at hand. It tells you the number of days in a given month of a year. So obviously when you run the code you need to have a year value and the month value as given values.

In this case month value and year value as provided during code execution time become the argument. In this case the word "argument" is used as such but you can use x or y or xyz to name a variable, as you know.

Java accepts the arguments as the String array. So prior to using them as Integer you need to parse them, that's what has been done in the above code.

Eg

 class WelcomeYouself{

    public static void main(String[] args){ //Here insted of arguments,the word args is used. 

    System.out.println("Hello " + args[0]);
  }
}

Now when you run this you pass your own name as argument.

   java WelcomeYourself Feynman; 

 // This how you run or execute the java code passing your name as the "argument". Of course it is presumed you are Feynman.

Upvotes: 1

dngfng
dngfng

Reputation: 1943

Arguments is a list of Parameters that can be passed to your Java Programm at start up.

if (arguments.length > 0) checks if any arguments have been provided.

As otherwise you will be trying to access an empty array and get and index out of bounds exception.

Also there are pleanty of tutorials out there, that can help you.

Have a look at Oracle's essentials guide, here about CMD Line Arguments.

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

- Usually parameters and arguments are used interchangeably, but they are different.

I will take an example to explain this:

public class Test{


   public void go(String s){


   }

   public static void main(String[] args){

      Test t = new Test();
      t.go("Hello");

    }

}

- In the above code variable s which is of type String in the line public void go(String s) is the Parameter.

- Where as "Hello" which is of type String in the line t.go("Hello") is an Argument.

- The elements in method definition or declaration are Parameters, where as the elements passed in the method call are Arguments.

Upvotes: 2

Redivider
Redivider

Reputation: 144

In this case arguments is the variable name used for array of Strings you provide as input on execution,

i.e.

java DayCounter 1 2010

In the following code excerpt:

public static void main(String[] arguments)

String[] means an array of Strings with a variable name of arguments. Java uses this function prototype for main as default. See here for a tutorial: http://docs.oracle.com/javase/tutorial/getStarted/application/index.html

So when you reference length in arguments (arguments.length), you are looking "inside" the array of Strings finding the length of the array (using a built-in function of Java Strings to do so)

Upvotes: 3

daniel gratzer
daniel gratzer

Reputation: 53881

arguments are the command line options given to your java program when it runs. They are stored in an array so calling arguments.length gives the number of command line inputs to your program.

They are passed in like this on execution

java program argument1, argument2, argument3

In this case arguments.length would return 3 as there are 3 command line arguments.

Upvotes: 4

Tim
Tim

Reputation: 1344

They come from the command prompt. When you start to run a program, you can say:

java program arg1 arg2 ...argN

The args go immediately after the program name.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1501003

I can't understand the line "if (arguments.length > 0) what does "arguments" mean? where did it value came from?

It came from the method declaration:

public static void main(String[] arguments) {

That declares a parameter called arguments. For a normal method call, the caller specifies the argument, and that becomes the initial value of the parameter. For example:

int foo(int x) {
    System.out.println(x);
}
...
foo(10);

Here, 10 is the argument to the method, so it's the initial value for the x parameter.

Now a public static void method called main in Java is an entry point - so when you run:

java MyClass x y z

the JVM calls your main method with an array containing the command line arguments - here, it would be an array of length 3, with values "x", "y" and "z".

For more details, see the relevant bits of the Java tutorial:

Upvotes: 8

Daniel Casserly
Daniel Casserly

Reputation: 3500

arguments is passed in to the main method

 public static void main(String[] arguments)

in this case it means an array of values that can be passed to this method. Usually this is the arguments that you pass to a program from command line or from a shortcut and then you can use them in the program to change the logic flow.

Upvotes: 1

Related Questions