0

public static void main(String[] args) I know conventionally the main function takes in a parameter args that contains the supplied command line arguments as an array of String objects.

  • I have not see main takes in any parameter other than String[] args. Why not a String or an array of Integer?
  • If there is a way to specify input for the main function, please provide an example.
2
  • 4
    Because command line arguments are a series of strings, so they're given as a string array. You can parse them as integers inside your program if that's what you want. Commented May 1, 2017 at 15:48
  • 2
    It can be String[] or String..., and nothing else, because that's what the JLS says it has to be. Commented May 1, 2017 at 15:51

5 Answers 5

3

Answer to your question: NO

Details:

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

The java command starts a Java application. It does this by starting a Java runtime environment, loading a specified class, and calling that class's main method.

The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration has the following form:

public static void main(String[] args)

Sign up to request clarification or add additional context in comments.

Comments

2

As you already know that is the method definition of main and you cannot change it. However, you can convert the elements within the args array to any other primitive type considering that it's in a valid format to be converted to the type on the left side of the assignment operator.

There are multiple ways to achieve this, one of which is:

int input = Integer.valueOf(args[0]);

Comments

1

No, your main method cannot take just a String, main method must accept an array of String using array syntax or varagrs syntax, read following JSL §12.1.4. Invoke Test.main

Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)

So, you can opt for either syntax but not just a String.

Just for your info you can create a method like public static void main(String args) but it wouldn't be regarded as "main" method of the class which JVM looks as the starting point. So, if you want to have a "main" method in your class then you have to choose from either of the above syntax.

Comments

1

Why not a String or an array of Integer?

Not an array of Integer because that array will cover only a subset of possible arguments (You can parse them and get an Integer, but you also need to check if they are Integers otherwise you'll get a NumberFormatException).

Not a string instead of a String array because it's better to have the command arguments separated instead of having them in one big string, in fact, a lot of times you need to handle the command arguments separately.

There's no alternative signature that the VM will recognize as main method. There is only one main signature allowed that is public static void main(String[] args)

Comments

0

I have not see main takes in any parameter other than String[] args. Why not a String or an array of Integer?

Yes, you can take any String or Integer as parameter, but that would become a different function main() method instead of the one used by java to start your program. After compilation, java search for public static void main(String[] args){} method to start with. If you change parameters then your program won't run at all. Now using array will cover only particular type of values that array can permit like int[] will only store intvalues, similarly String can have only one value, while you may want to have multiple arguments.

If there is a way to specify input for the main function, please provide an example.

Yes, there are ways to pass argument using command line(for windows) and terminal(for linux/unix).

  1. compile your java program by using javac Filename.java
  2. run java program along with parameters java FileName param1 param2 param3 (param1, param2, param3 are parameters that you want to pass).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.