3

I was wondering why do Java programs take parameters only in the form on Strings from the command line when they are run? I have read that if the parameters are declared as int, it still converts the int into String. Why does this happen? And is there any way to accept only int values from the command line when running a java program?

2
  • 1
    The link you provided seems to answer your question Commented Apr 28, 2012 at 17:51
  • You haven't read that at all. See Jon Skeet'e comment in your link. Commented Apr 29, 2012 at 1:14

3 Answers 3

3

Strings are more versatile as they can hold any value and can even be an representative as an int. If you want to pass an int in the command line you can convert the string to and int yourself.

int val = Integer.parseInt(arg[0]);

The valid signature for the main method is

public static void main(String[] args)

no other argument structure will be seen as a main function.

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

Comments

1

I have read that if the parameters are declared as int, it still converts the int into String.

You have read that incorrectly. If the main() method takes anything other String[] args, it won't be recognized as a valid entry point by the JVM.

If you wish to take integer arguments, your main method still has to accept String[] args, and you have to perform the conversion yourself.

Comments

0

You passing chars from command line anyway (just because there is no "integers" on keyboard). So, you need to convert this char arrays (which are wrapped into String) into integers.

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.