0

In java ,main method consists of (String[]args) why it can't accept integer arguments?

7th Sep 2022, 1:12 AM
Tathvik
Tathvik - avatar
2 odpowiedzi
+ 4
Tathvik Whenever you take user input then it is considered as string input so if you take input 2 then it is actually a string not integer. To convert input to integer we use Integer.parseInt(). So here args is a string array which can contains any type of value but that would be string. So args accepts every type of arguments even integer also but you need to parse like int a = Integer.parseInt(args[0]);
7th Sep 2022, 5:56 AM
A͢J
A͢J - avatar
+ 3
main method is a special method . its pre defined by java like that. cli (command line interface) arguments and other environment variables are passed by jvm to your program through that args string array . you can try printing it to know more about it... https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
7th Sep 2022, 3:42 AM
Prashanth Kumar
Prashanth Kumar - avatar