Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can't int array agrument in main method??  RSS feed

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Why the main method is only accepting String array argument?

public static void main(String args[]){
}

Why can't we use int array argument? if I am comppiling, it is compiled, but while try to run it gives:


What is the reason behind this?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when you execute your application the main(String[] args) is executed. The
string array is a list of arguments you can pass when you execute it from
the console.

E.g. >java MyJavaApp svend rost
will execute the main(String[] args) method, where args will contain two
elements ("svend" and "rost").

In other words, you get the java.lang.NoSuchMethodError: main
exception because it can not find the "correct" main method.

/Svend Rost
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to send like this:
java Mypgm 1 2 3

I know we can convert this String 1,2,3 to int...
but my question is why can't int arg[]..in main()
why they designed main should only have String array?
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Micheal John:

my question is why can't int arg[]..in main()



becuase main() method only accepts a single parameter: an array of Strings.
[ November 30, 2006: Message edited by: Saif uddin ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why they designed main should only have String array?

Because if both "public static void main(String[] args)" and "public static void main(int[] args)" were permitted, you would get all kinds of strange issues. Suppose you write a program that has both of the methods:

Which one of the two main methods should be called when your program starts? Before you say "if I enter only numbers on the command line, then I want the second one, otherwise the first one":

If the String[] as the int[] version would be allowed, then there would also be no reason to not have a version of main() that takes a char[], a long[] etc. and the question about which one should be called when will be even more complicated.

In other words, to keep Java simple there is just one version of the main() method that can be used as the entry point for the program, and that's the one that takes a String[].
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just my personal opinion, and others may differ, but ...

I think, when learning a new language, it pays not to ask too many "why does the language [not] have feature X?". The answers will often be difficult to understand, as a newcomer to the language. Instead, get some familiarity with the language, and the ways of thinking that are associated with it. Often, features or omissions that seem ridiculous initially turn out not to be bothersome at all, once you are thinking the right way for the language.

Once familiar with the language, it is then interesting to ask these types of question. Sometimes, there is a plain good reason for the feature or omission (as in your question). Sometimes, it is a result of a balance between competing demands (e.g. lack of multiple inheritance). Sometimes, the feature or omission is just bone-headed (Cloneable, finalize(), Calendar...).

Finally, I'll pseudo-quote a strap-line from another Rancher. "There are two types of language: the ones no-one likes and the ones no-one uses". Mind you, that was being used to defend C++, which (no troll intended) is just plain rubbish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!