Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input parameters in java

Tags:

java

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?

like image 476
arkiver Avatar asked Dec 11 '25 12:12

arkiver


1 Answers

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.

like image 72
twain249 Avatar answered Dec 14 '25 00:12

twain249